public override string Output() { Application word = new ApplicationClass { Visible = false }; Document doc = word.Documents.Add(); doc.PageSetup.TopMargin = word.CentimetersToPoints(3.7F); doc.PageSetup.BottomMargin = word.CentimetersToPoints(3.5F); doc.PageSetup.LeftMargin = word.CentimetersToPoints(2.8F); doc.PageSetup.RightMargin = word.CentimetersToPoints(2.6F); doc.Paragraphs.Last.Range.Text = "目 录"; foreach (var item in _outline) { doc.Paragraphs.Last.Range.Text += item.title + '\t' + item.page; } Selection cursor = word.Selection; cursor.WholeStory(); cursor.ParagraphFormat.SpaceBeforeAuto = 0; cursor.ParagraphFormat.SpaceAfterAuto = 0; cursor.ParagraphFormat.LeftIndent = 0; cursor.ParagraphFormat.FirstLineIndent = 0; cursor.ParagraphFormat.CharacterUnitFirstLineIndent = 0; cursor.ParagraphFormat.WordWrap = 0; cursor.ParagraphFormat.FirstLineIndent = 0; cursor.ParagraphFormat.TabStops.ClearAll(); cursor.ParagraphFormat.TabStops.Add(Position: word.CentimetersToPoints(14.6F), Alignment: WdHorizontalLineAlignment.wdHorizontalLineAlignLeft, Leader: WdTabLeader.wdTabLeaderDots); //将wordDoc文档对象的内容保存为DOCX文档 string dst_filepath = GetTocName("docx"); doc.SaveAs2(dst_filepath); doc.Close(); //关闭wordApp组件对象 word.Quit(); return(dst_filepath); }
public void AddTable(ApplicationClass WordApp, Document aDoc, DataTable dt, string[] Caption, string[] FieldName, Dictionary <int, float> colWidth) { wrdRng = aDoc.Bookmarks.get_Item(ref oEndOfDoc).Range; oTable = aDoc.Tables.Add(wrdRng, dt.Rows.Count + 1, FieldName.Length, ref oMissing, ref oMissing); oTable.Range.Font.Bold = 0; oTable.Range.Font.Italic = 0; oTable.Range.Font.Underline = 0; oTable.Range.ParagraphFormat.SpaceAfter = 2; oTable.Range.Cells.Borders.Enable = 1; int c; // Set Caption for (c = 1; c <= FieldName.Length; c++) { oTable.Cell(1, c).Range.Text = Caption[c - 1]; if (colWidth.ContainsKey(c)) { oTable.Columns[c].Width = WordApp.CentimetersToPoints(colWidth[c]); } } // Fill content for (int r = 1; r <= dt.Rows.Count; r++) { for (c = 1; c <= FieldName.Length; c++) { if (dt.Columns.Contains(FieldName[c - 1])) { oTable.Cell(r + 1, c).Range.Text = GetText(dt.Rows[r - 1][FieldName[c - 1]], dt.Columns[FieldName[c - 1]].DataType.Name); } } } oTable.Rows[1].Range.Font.Bold = 1; }
public void InsertLine(float left, float top, float width, float weight, int r, int g, int b) { //SetFontColor("red"); //SetAlignment("Center"); object Anchor = oWordApplic.Selection.Range; //int pLeft = 0, pTop = 0, pWidth = 0, pHeight = 0; //oWordApplic.ActiveWindow.GetPoint(out pLeft, out pTop, out pWidth, out pHeight,missing); //MessageBox.Show(pLeft + "," + pTop + "," + pWidth + "," + pHeight); object rep = false; //left += oWordApplic.ActiveDocument.PageSetup.LeftMargin; left = oWordApplic.CentimetersToPoints(left); top = oWordApplic.CentimetersToPoints(top); width = oWordApplic.CentimetersToPoints(width); Microsoft.Office.Interop.Word.Shape s = oWordApplic.ActiveDocument.Shapes.AddLine(0, top, width, top, ref Anchor); s.Line.ForeColor.RGB = RGB(r, g, b); //s.Line.Visible = Microsoft.Office.Core.MsoTriState.msoTrue; //s.Line.Style = Microsoft.Office.Core.MsoLineStyle.msoLineSingle; s.Line.Weight = weight; }
public static Boolean CreateReportPDFFromExcel(string excelFilePath, String pdfFilePath, Int32 reportIndex) { Boolean flag = false; if (!System.IO.File.Exists(excelFilePath)) { return(flag); } ApplicationClass excelApplication = new ApplicationClass(); Workbook excelWorkBook = null; string paramSourceBookPath = excelFilePath; object paramMissing = Type.Missing; XlFixedFormatType paramExportFormat = XlFixedFormatType.xlTypePDF; XlFixedFormatQuality paramExportQuality = XlFixedFormatQuality.xlQualityMinimum; bool paramOpenAfterPublish = false; bool paramIncludeDocProps = true; bool paramIgnorePrintAreas = true; object paramFromPage = 1; object paramToPage = 1; try { // Open the source workbook. excelWorkBook = excelApplication.Workbooks.Open(paramSourceBookPath, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing, paramMissing); // Save it in the target format. if (excelWorkBook != null) { if (!Directory.Exists(Path.GetDirectoryName(pdfFilePath))) { Directory.CreateDirectory(Path.GetDirectoryName(pdfFilePath)); } Worksheet reportSheet = excelWorkBook.Sheets[reportIndex] as Worksheet; if (reportSheet != null) { if (File.Exists(pdfFilePath)) { File.Delete(pdfFilePath); } reportSheet.PageSetup.Zoom = 90; reportSheet.PageSetup.LeftMargin = excelApplication.CentimetersToPoints(3); reportSheet.PageSetup.RightMargin = excelApplication.CentimetersToPoints(2); reportSheet.PageSetup.TopMargin = excelApplication.CentimetersToPoints(2.5); reportSheet.PageSetup.BottomMargin = excelApplication.CentimetersToPoints(2.5); reportSheet.ExportAsFixedFormat(paramExportFormat, pdfFilePath, paramExportQuality, paramIncludeDocProps, paramIgnorePrintAreas, paramFromPage, paramToPage, paramOpenAfterPublish, paramMissing); } } flag = true; } catch (Exception e) { throw e; } finally { // Close the workbook object. if (excelWorkBook != null) { excelWorkBook.Close(false, paramMissing, paramMissing); excelWorkBook = null; } // Close the ApplicationClass object. if (excelApplication != null) { excelApplication.Quit(); excelApplication = null; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } return(flag); }