c# WPS DLL及其调用
1.dll分享(含xsl及docx的dll)
链接:https://pan.baidu.com/s/1c1ImV14OndmvIb4W-_WL2A 密码:d2rx
2.方法:
1.先在类的前面(类外面)添加引用:
using MSWord = Microsoft.Office.Interop.Word;
2.初始化
public static object oMissing; private Excel.Application oXL; private Excel.Workbook oWB; private Excel.Worksheet oSheet; private Excel.ChartObject chartObject; private MSWord._Application oWord; private MSWord._Document oDoc; private MSWord.Range rang = null; #region 初始化Office /// <summary> /// 初始化Office /// </summary> private void InitOffice() { oMissing = System.Reflection.Missing.Value; #region 实例化一个Excel.Application对象 oXL = new Excel.Application(); oXL.Visible = false; //设置为不可见 //String strFileName = Application.StartupPath + "\\compare.xls"; //模板文件地址 String strFileName = ByteConvertWord(Properties.Resources.compare, "2"); oWB = oXL.Workbooks.Add(strFileName); //以模板为基础生成文档 oSheet = oWB.ActiveSheet as Excel.Worksheet; #endregion if (Directory.Exists(Application.StartupPath + "\\tempImg") == false)//如果不存在就创建file文件夹 { Directory.CreateDirectory(Application.StartupPath + "\\tempImg"); } #region 创建一个Word应用程序实例 oWord = new MSWord.Application(); oWord.Visible = false; //设置为不可见 //模板文件地址,这里假设在X盘根目录 //object oTemplate = Application.StartupPath + "\\comparisonTCM.docx"; object oTemplate = ByteConvertWord(Properties.Resources.comparisonTCM, "1"); //以模板为基础生成文档 oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing); #endregion } #endregion /// <summary> /// 二进制数据转换为word文件 /// </summary> /// <param name="data">二进制数据</param> /// <param name="fileName">word文件名</param> /// <returns>word保存的相对路径</returns> public string ByteConvertWord(byte[] data, string fileName) { string savePath = @"\\" + fileName + ".gif"; string filePath = Application.StartupPath + savePath; FileStream fs; if (System.IO.File.Exists(filePath)) { fs = new FileStream(filePath, FileMode.Truncate); } else { fs = new FileStream(filePath, FileMode.CreateNew); } BinaryWriter br = new BinaryWriter(fs); br.Write(data, 0, data.Length); br.Close(); fs.Close(); return filePath; }
3.开始写(可以设置字体,大小)
//excel表格转为图片并添加至word
oSheet.Cells[i - 14, 10] = Math.Round(data_list[i][0], 2);
oSheet.Cells[i - 14, 12] = data_list[i][4];
chartObject = (Excel.ChartObject)oSheet.ChartObjects("图表 解剖2");
string dissection_2_chart_image = @"" + Application.StartupPath + "\\tempImg\\dissection_2_chart_image.png";
chartObject.Chart.Export(dissection_2_chart_image, "png");
oDoc.Bookmarks.get_Item("dissection_front_chart").Select();
oDoc.Application.Selection.InlineShapes.AddPicture(dissection_chart_image);
oDoc.Bookmarks.get_Item("dissection_back_chart").Select();
oDoc.Application.Selection.InlineShapes.AddPicture(dissection_2_chart_image);
//word中的文本框获取及插入 oDoc.Bookmarks.get_Item("img_A1").Select(); MSWord.InlineShape inlineShape = oDoc.Application.Selection.InlineShapes.AddPicture(@"" + Application.StartupPath + "\\tempImg\\F_Image_One.png"); oDoc.Bookmarks.get_Item("img_A2").Select(); inlineShape = oDoc.Application.Selection.InlineShapes.AddPicture(@"" + Application.StartupPath + "\\tempImg\\B_Image_One.png"); oDoc.Bookmarks.get_Item("img_B1").Select(); inlineShape = oDoc.Application.Selection.InlineShapes.AddPicture(@"" + Application.StartupPath + "\\tempImg\\F_Image_Two.png"); oDoc.Bookmarks.get_Item("img_B2").Select(); inlineShape = oDoc.Application.Selection.InlineShapes.AddPicture(@"" + Application.StartupPath + "\\tempImg\\B_Image_Two.png"); //文字添加 oDoc.Bookmarks.get_Item("home_name").Range.Text = cim[0].customername;
//属性设置
rang.Font.Color = MSWord.WdColor.wdColorRed;
4.保存
private void SaveAndClose() { try { //设置禁止弹出保存和覆盖的询问提示框 oXL.DisplayAlerts = false; object paramMissing = Type.Missing; string paramExportFilePath = Application.StartupPath + @"\tmpA\document_Ctmp.pdf"; //string paramExportFilePath = Application.StartupPath + @"\tempImg\comparisonReportTCM.pdf"; MSWord.WdExportFormat paramExportFormat = MSWord.WdExportFormat.wdExportFormatPDF; MSWord.WdExportOptimizeFor paramExportOptimizeFor = MSWord.WdExportOptimizeFor.wdExportOptimizeForPrint; MSWord.WdExportRange paramExportRange = MSWord.WdExportRange.wdExportAllDocument; MSWord.WdExportItem paramExportItem = MSWord.WdExportItem.wdExportDocumentContent; MSWord.WdExportCreateBookmarks paramCreateBookmarks = MSWord.WdExportCreateBookmarks.wdExportCreateWordBookmarks; oDoc.ExportAsFixedFormat(paramExportFilePath, paramExportFormat, false, paramExportOptimizeFor, paramExportRange, 0, 0, paramExportItem, true, true, paramCreateBookmarks, true, true, false, ref paramMissing); } catch (Exception exp) { MessageBox.Show(exp.ToString()); } finally { WReport.Close(oDoc, oWord, oSheet, oWB, oXL); } }