/// <summary> /// 将word保存为doc格式 /// </summary> /// <param name="o"></param> public void SaveDoc(object o) { string file = o as string; //保存文档为doc格式 string parent = Directory.GetParent(MySetting.Default.fieldirectory).FullName; string savedir = file.Replace(parent, ""); if (!Directory.Exists(savedir)) { Directory.CreateDirectory(savedir); } string filename = Path.GetFileNameWithoutExtension(file); string savepath = savedir + "\\" + filename + ".docx"; Spire.Doc.Document mydoc = new Spire.Doc.Document(); mydoc.SaveToFile(savepath, FileFormat.Docx); mydoc.Close(); //去水印 Aspose.Words.Document aspdoc = new Aspose.Words.Document(savepath); aspdoc.Sections[0].Body.Paragraphs.RemoveAt(0); aspdoc.Save(savedir + "\\" + filename + ".doc", Aspose.Words.SaveFormat.Doc); //删除docx文件 File.Delete(savepath); }
private void BtnDoc_Click(object sender, RoutedEventArgs e) { SaveFileDialog fileDialog = new SaveFileDialog(); Person person = new Person(); string path = person.FilePath; fileDialog.Filter = "Text documents (.doc)|*.doc"; fileDialog.Title = "Save an Doc File"; if (fileDialog.ShowDialog() == true) { string fileName = fileDialog.FileName; string extesion = System.IO.Path.GetExtension(fileName); switch (extesion) { case ".doc": //do something here Spire.Doc.Document document = new Spire.Doc.Document(); document.LoadText(path); document.SaveToFile(fileName, FileFormat.Doc); document.Close(); MessageBox.Show("Conversion Successful...."); break; } } /* * document.SaveToFile(System.IO.Path.Combine(path, "TestWordDoc.docx"), FileFormat.Doc); * document.LoadFromFile(System.IO.Path.Combine(path, "TestTxt.txt")); * * document.LoadFromFile(System.IO.Path.Combine(docPath, "TestWordDoc.docx")); * string readText = File.ReadAllText(System.IO.Path.Combine(docPath, "TestTxt.txt")); */ }
private void BtnPdf_Click(object sender, RoutedEventArgs e) { SaveFileDialog fileDialog = new SaveFileDialog(); Person person = new Person(); string path = person.FilePath; string imgPath = person.ImagePath; fileDialog.Filter = "Text documents (.pdf)|*.pdf"; fileDialog.Title = "Save an Pdf File"; if (fileDialog.ShowDialog() == true) { string fileName = fileDialog.FileName; string extesion = System.IO.Path.GetExtension(fileName); switch (extesion) { case ".pdf": Spire.Doc.Document document = new Spire.Doc.Document(); document.LoadText(path); document.SaveToFile(fileName, FileFormat.PDF); document.Close(); MessageBox.Show("Conversion Successful...."); break; } } // this.Close(); }
private void CreateDocument(object o) { ExcelInfo ei = o as ExcelInfo; dgv_task.Rows[ei._id - 1].Cells[4].Value = "正在准备"; //获得 table DataTable dt_split = ei._data; //读取excel,得到行数 /*生成一个新的word*/ Spire.Doc.Document spdoc = new Spire.Doc.Document(); /*循环把list中的mycu内容添加到word文档*/ for (int rowindex = 0; rowindex < dt_split.Rows.Count; rowindex++) { dgv_task.Rows[ei._id - 1].Cells[4].Value = (100 * Convert.ToDecimal(rowindex + 1) / Convert.ToDecimal(dt_split.Rows.Count)).ToString("00.00") + "%"; //更新状态栏文字 //string str_statue = "正在生成第{0}个,共{1}个……"; //UpdateStatue(string.Format(str_statue, rowindex - 1, lastrow - 1)); ///载入一个空白文档 spdoc.LoadFromFile(Environment.CurrentDirectory + @"\newdoc.docx"); for (int i = Global.list_myuc.Count - 1; i >= 0; i--) { //设置文本 string str_n = string.Empty; //设置空行 for (int ji = 0; ji < Global.list_myuc[i].mysetter.konghang; ji++) { str_n += "\n"; } //添加一个段落 Spire.Doc.Documents.Paragraph parainsert = spdoc.LastSection.AddParagraph(); //Spire.Doc.Documents.Paragraph parainsert = spdoc.CreateParagraph(); TextRange tx = parainsert.AppendText(GetContent(Global.list_myuc[i].mysetter.listcolumn, rowindex, dt_split) + str_n); //字体名称 tx.CharacterFormat.FontName = Global.list_myuc[i].mysetter.fontname; //字体大小 tx.CharacterFormat.FontSize = Global.list_myuc[i].mysetter.fontsize; //设置行距 switch (Global.list_myuc[i].mysetter.hangjustyle) { case "单倍行距": parainsert.Format.LineSpacingRule = Spire.Doc.LineSpacingRule.AtLeast; break; case "1.5倍行距": parainsert.Format.LineSpacingRule = Spire.Doc.LineSpacingRule.Exactly; break; case "2倍行距": parainsert.Format.LineSpacingRule = Spire.Doc.LineSpacingRule.Multiple; break; default: parainsert.Format.LineSpacingRule = Spire.Doc.LineSpacingRule.Exactly; parainsert.Format.LineSpacing = Global.list_myuc[i].mysetter.hangjuvalue; break; } //首行缩进 parainsert.Format.SetFirstLineIndent(Global.list_myuc[i].mysetter.suojin); //首行缩进 //粗体 tx.CharacterFormat.Bold = Global.list_myuc[i].mysetter.bold == 1 ? true : false; switch (Global.list_myuc[i].mysetter.juzhong) { case "左对齐": parainsert.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left; break; case "居中": parainsert.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; break; case "右对齐": parainsert.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Right; break; } } SectionCollection sections = spdoc.Sections; //创建一个HeaderFooter类实例,添加页脚 for (int i = 0; i < sections.Count; i++) { sections[i].PageSetup.Margins.Top = 72f; sections[i].PageSetup.Margins.Left = 90f; sections[i].PageSetup.Margins.Bottom = 72f; sections[i].PageSetup.Margins.Right = 90f; Spire.Doc.HeaderFooter footer = sections[i].HeadersFooters.Footer; Spire.Doc.Documents.Paragraph footerPara = footer.AddParagraph(); //添加字段类型为页码,添加当前页、分隔线以及总页数 footerPara.AppendField("页码", FieldType.FieldPage); footerPara.AppendText(" / "); footerPara.AppendField("总页数", FieldType.FieldNumPages); footerPara.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; } /*保存文档*/ //组成文件名 List <string> strfilename = new List <string>(); for (int i = 0; i < Global.listfilename.Count; i++) { string filename_element = Regex.Replace(dt_split.Rows[rowindex][Global.listfilename[i] + 1].ToString(), @"[\s/\:*?''<>|]", ""); strfilename.Add(filename_element); } spdoc.BuiltinDocumentProperties.Author = "潜挖智库"; spdoc.SaveToFile(tbcunfang.Text + @"\" + string.Join("-", strfilename) + @".docx"); spdoc.Close(); } }
public void Export(DataGridView dataGrid, string filename) { Spire.Doc.Document document = new Spire.Doc.Document(); Spire.Doc.Section section = document.AddSection(); section.PageSetup.Orientation = Spire.Doc.Documents.PageOrientation.Landscape; AddHeaderTitle(section); Spire.Doc.Table table = section.AddTable(true); String[] header = { "ID", "Full Name", "Date of birth", "Gender", "Phone", "Email", "Position" }; int RowCount = dataGrid.Rows.Count; int ColumnCount = dataGrid.Columns.Count; String[][] data = new String[RowCount][]; for (int i = 0; i < RowCount; i++) { data[i] = new String[ColumnCount]; } //add rows for (int c = 0; c < ColumnCount; c++) { for (int r = 0; r < RowCount; r++) { if (dataGrid.Rows[r].Cells[c].Value.GetType() == typeof(DateTime)) { data[r][c] = ((DateTime)dataGrid.Rows[r].Cells[c].Value).ToString("dd/MM/yyyy"); } else { data[r][c] = dataGrid.Rows[r].Cells[c].Value.ToString(); } } } table.ResetCells(data.Length + 1, header.Length); TableRow FRow = table.Rows[0]; FRow.IsHeader = false; FRow.Height = 23; //FRow.RowFormat.BackColor = Color.AliceBlue; for (int i = 0; i < header.Length; i++) { //Cell Alignment Spire.Doc.Documents.Paragraph p = FRow.Cells[i].AddParagraph(); FRow.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle; p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; //Data Format Spire.Doc.Fields.TextRange TR = p.AppendText(header[i]); TR.CharacterFormat.FontName = "Calibri"; TR.CharacterFormat.FontSize = 14; TR.CharacterFormat.TextColor = Color.Black; TR.CharacterFormat.Bold = true; } //Data Row for (int r = 0; r < data.Length; r++) { TableRow DataRow = table.Rows[r + 1]; //Row Height DataRow.Height = 20; //C Represents Column. for (int c = 0; c < data[r].Length; c++) { //Cell Alignment DataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle; //Fill Data in Rows Spire.Doc.Documents.Paragraph p2 = DataRow.Cells[c].AddParagraph(); Spire.Doc.Fields.TextRange TR2 = p2.AppendText(data[r][c]); //Format Cells p2.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center; TR2.CharacterFormat.FontName = "Calibri"; TR2.CharacterFormat.FontSize = 12; TR2.CharacterFormat.TextColor = Color.Black; } } //Save and Launch document.SaveToFile(filename); MessageBox.Show("Document Created Successfully!", "Export File", MessageBoxButtons.OK, MessageBoxIcon.Information); document.Close(); }