C#保存doc文件

1、使用office组件(Microsoft.Office.Interop.Word)

https://www.cnblogs.com/Joyce-mi7/p/17445396.html

2、使用免费的 Spire.Doc

https://www.cnblogs.com/HoFei/p/17425140.html

3、使用DocX插件

https://blog.csdn.net/zhanfu2905/article/details/68948002

https://www.cnblogs.com/echo-web/p/9511119.html

https://blog.csdn.net/ot512csdn/article/details/120987418

string fileName = @"../../" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
File.Create(fileName).Close();
using (var document = DocX.Create(fileName))
{    
    var title = document.InsertParagraph().Append("测试结果").Bold().FontSize(20); //标题加粗
    title.Alignment = Alignment.center;//标题居中
    document.InsertParagraph().Append("——测试类型为:xxxx").FontSize(16); //添加段落

    // 将图像添加到文档中。   
    string imgpath = "C:\\Users\\1111.jpg";
    var image = document.AddImage(imgpath);
    var picture = image.CreatePicture();
    var s = document.InsertParagraph().AppendPicture(picture);
    s.Alignment = Alignment.center;

    //添加表格
    Table table = document.AddTable(行, 列).Alignment = Alignment.center;
    table.Rows[0].MergeCells(0, 2); //合并单元格(3格)MergeCells(开始索引, 结束索引)
    table.Rows[0].Cells[0].Paragraphs[0].Append("测试类型").Bold().Alignment = Alignment.center;
    table.Rows[0].MergeCells(1, 2); //合并单元格(2格),注意前面合并之后,这里开始的索引和原来表格的索引不一样了。
    table.Rows[0].Cells[1].Paragraphs[0].Append(dataSet.Tables[0].Rows[0][2].ToString()).Alignment = Alignment.center;
    //dataSet.Tables[0].Rows[0][2].ToString()表示从数据库中查询出来的数据

    document.InsertTable(table); //保存表格

    document.InsertParagraph().Append("测试时间" + DateTime.Now).FontSize(16).Alignment = Alignment.right;
    foreach (var paragraph in document.Paragraphs) //更改行距1.5倍
    {
          paragraph.LineSpacing = (float)18;
    }
    // 保存当前文档
    document.Save();
}

4、打印文件

public  static void Print(string fileName)
{
   try
   {
       PrintDocument fpd = new PrintDocument();

       string filePath = fileName;
       string printer = fpd.PrinterSettings.PrinterName; //获取默认的打印机
       ProcessStartInfo info = new ProcessStartInfo();
       info.Arguments = "\"" + printer + "\"";
       info.Verb = "PrintTo";
       info.FileName = filePath;
       info.CreateNoWindow = true;
       info.WindowStyle = ProcessWindowStyle.Hidden;

       Process p = new Process();
       p.StartInfo = info;
       p.Start();
       p.WaitForInputIdle();
   }
   catch (Exception ex)
   {
       Console.Error.WriteLine(ex.Message);
   }
}

https://blog.csdn.net/BYH371256/article/details/130898529 

posted @ 2023-06-28 15:52  浑浑噩噩一只小迷七  阅读(172)  评论(0)    收藏  举报