基于Microsoft.Office.Interop.Word的导出

public void WriteWord()
{
Object Nothing = Missing.Value; //由于使用的是COM库,因此有许多变量需要用Missing.Value代替
object format = MSWord.WdSaveFormat.wdFormatDocumentDefault;
object unite = Microsoft.Office.Interop.Word.WdUnits.wdStory;
string ACellNameChecked = "";
object InSysInterAnalyReportWordSavePath;
MSWord._Application InSysInterAnalyReportWordApp;//Word应用程序变量初始化
MSWord.Document InSysInterAnalyReportWordDoc;
InSysInterAnalyReportWordApp = new MSWord.Application();//Word应用程序变量初始化
InSysInterAnalyReportWordDoc = InSysInterAnalyReportWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); //新建一个word文档对象

string ConfidenceLevelResulInfo = "";// 优化方案信息文字
//表格
MSWord.Table AcellnameCellInfoTable;
MSWord.Table NcellnameCellInfoTable1;
MSWord.Table NcellnameCellInfoTable2;
MSWord.Table NcellnameCellInfoTable3;

ACellNameChecked = InvalidCellNameCharsRemoveForFilePath(AnalysisCellNamecomboBox.Text);
if (!Directory.Exists(InterferenceAnalysisResultSavePath + "\各小区优化方案详细信息"))
Directory.CreateDirectory(InterferenceAnalysisResultSavePath + "\各小区优化方案详细信息");
InSysInterAnalyReportWordSavePath = InterferenceAnalysisResultSavePath + "\各小区优化方案详细信息" + "\" + ACellNameChecked + "各小区优化方案详细信息.docx";

//整个文档的文字设置(Selection表示当前选择集,如果当前没有选择对像,则指对光标所在处进行设置)
InSysInterAnalyReportWordApp.Selection.Font.Name = "宋体";//字体设置
InSysInterAnalyReportWordApp.Selection.Font.Bold = 0; //不加粗
InSysInterAnalyReportWordApp.Selection.Font.Color = MSWord.WdColor.wdColorBlack; //字体颜色设置
InSysInterAnalyReportWordApp.Selection.ParagraphFormat.LineSpacingRule = MSWord.WdLineSpacing.wdLineSpace1pt5; //1.5倍行间距

//标题生成
InSysInterAnalyReportWordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; //居中对齐
InSysInterAnalyReportWordApp.Selection.Font.Size = 16f;//三号字体
InSysInterAnalyReportWordApp.Selection.ParagraphFormat.FirstLineIndent = 0; //首行无缩进
InSysInterAnalyReportWordDoc.Paragraphs.Last.Range.Text = AnalysisCellNamecomboBox.Text + " ";
InSysInterAnalyReportWordDoc.Paragraphs.Last.Range.Select(); //光标移至文末

#region 受扰小区名称生成
InSysInterAnalyReportWordDoc.Paragraphs.Last.OutlineLevel = MSWord.WdOutlineLevel.wdOutlineLevelBodyText;
InSysInterAnalyReportWordApp.Selection.Font.Size = 10.5f;//五号字体
InSysInterAnalyReportWordApp.Selection.ParagraphFormat.FirstLineIndent = 0; //首行缩进2字符
ConfidenceLevelResulInfo = OptimizeDictionaryAl[AnalysisCellNamecomboBox.SelectedItem.ToString()].Keys.ToList()[0];// 优化方案信息文字
InSysInterAnalyReportWordDoc.Paragraphs.Last.Range.Text = "受扰小区:" + ConfidenceLevelResulInfo + " "; ;
InSysInterAnalyReportWordDoc.Paragraphs.Last.Range.Select(); //光标移至文末


InSysInterAnalyReportWordApp.Selection.ParagraphFormat.LineSpacingRule = MSWord.WdLineSpacing.wdLineSpaceSingle; //单倍行间距
InSysInterAnalyReportWordApp.Selection.Font.Size = 7.5f;//7.5字体
InSysInterAnalyReportWordApp.Selection.EndKey(ref unite, ref Nothing); //光标移至文末
AcellnameCellInfoTable = InSysInterAnalyReportWordDoc.Tables.Add(InSysInterAnalyReportWordApp.Selection.Range, 4, 3);
AcellnameCellInfoTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
AcellnameCellInfoTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
AcellnameCellInfoTable.Select();//选中表格
InSysInterAnalyReportWordApp.Selection.Tables[1].Rows.Alignment = Microsoft.Office.Interop.Word.WdRowAlignment.wdAlignRowCenter;//表格居中
AcellnameCellInfoTable.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
//单元格信息填充
AcellnameCellInfoTable.Cell(1, 1).Range.Text = checkBox1.Text;
AcellnameCellInfoTable.Cell(1, 2).Range.Text = textBox3.Text;
AcellnameCellInfoTable.Cell(1, 3).Range.Text = textBox21.Text;
AcellnameCellInfoTable.Cell(2, 1).Range.Text = checkBox2.Text;
AcellnameCellInfoTable.Cell(2, 2).Range.Text = textBox4.Text;
AcellnameCellInfoTable.Cell(2, 3).Range.Text = textBox20.Text;
AcellnameCellInfoTable.Cell(3, 1).Range.Text = checkBox3.Text;
AcellnameCellInfoTable.Cell(3, 2).Range.Text = textBox5.Text;
AcellnameCellInfoTable.Cell(3, 3).Range.Text = textBox19.Text;
AcellnameCellInfoTable.Cell(4, 1).Range.Text = checkBox6.Text;
AcellnameCellInfoTable.Cell(4, 2).Range.Text = textBox10.Text;
AcellnameCellInfoTable.Cell(4, 3).Range.Text = textBox18.Text;
InSysInterAnalyReportWordApp.Selection.EndKey(ref unite, ref Nothing); //光标移至文末
#endregion

//将wordDoc 文档对象的内容保存为DOC 文档,并保存到path指定的路径
InSysInterAnalyReportWordDoc.SaveAs(ref InSysInterAnalyReportWordSavePath, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭wordDoc文档
InSysInterAnalyReportWordApp.Documents.Close(ref Nothing, ref Nothing, ref Nothing);
//关闭wordApp组件对象
InSysInterAnalyReportWordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}

原文地址:https://www.cnblogs.com/SiSui/p/9506089.html