pixiuz 2021-12-08 16:36 采纳率: 66.7%
浏览 60
已结题

C#操作word文档,根据模板随数据生成报告和表格

1.从datagirdview生成的表格总是新打开一个文档保存,而不是和内容一块儿保存在生成的新文档保存到本地。
2.书签替换内容可以实现,请教datagridview生成的表格怎么让他显示在报告的后面。


```c#
public bool ExportDataGridview(DataGridView dgv, bool isShowWord, string tempPath, string fileName)
{
            wordApp = new Microsoft.Office.Interop.Word.Application();
            //模板文件
            string TemplateFile = tempPath;
            //生成的具有模板样式的新文件
            string FileName = fileName;
            //模板文件拷贝到新文件
            File.Copy(TemplateFile, FileName);
            Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
            object Obj_FileName = FileName;
            object Visible = false;
            object missing = System.Reflection.Missing.Value;
            wordDoc = wordApp.Documents.Open(ref Obj_FileName, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing);
            //wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;//显示警报
            wordApp.Visible = true;
            Microsoft.Office.Interop.Word.Table mytable;
            Microsoft.Office.Interop.Word.Selection mysel;
            Object myobj;
            if (dgv.Rows.Count == 0)
                return false;
            //建立Word对象
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            myobj = System.Reflection.Missing.Value; 
            doc = word.Documents.Add(ref myobj, ref myobj, ref myobj, ref myobj);
            word.Visible = true;
            doc.Select();
            mysel = word.Selection;
            //将数据生成Word表格文件
            mytable = doc.Tables.Add(mysel.Range, dgv.RowCount, dgv.ColumnCount, ref myobj, ref myobj);
            mytable.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
            mytable.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
            mytable.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParag 
            raphCenter;//表格文本居中
            //输出列标题数据
            for (int i = 0; i < dgv.ColumnCount; i++)
            {
                mytable.Cell(1, i+1).Range.InsertAfter(dgv.Columns[i].HeaderText);
            }
            mytable.Cell(1, 3).Merge(mytable.Cell(1, 4));
            mytable.Cell(1, 1).Range.Font.Bold = 1;//加粗
            mytable.Cell(1, 1).Range.Font.Name = "黑体";//字体
            mytable.Cell(1, 1).Range.Font.Size = 12; //字体大小
            mytable.Cell(1, 2).Range.Font.Bold = 1;//加粗
            mytable.Cell(1, 2).Range.Font.Name = "黑体";//字体
            mytable.Cell(1, 2).Range.Font.Size = 12; //字体大小
            mytable.Cell(1, 3).Range.Font.Bold = 1;//加粗
            mytable.Cell(1, 3).Range.Font.Name = "黑体";//字体
            mytable.Cell(1, 3).Range.Font.Size = 12; //字体大小
            //输出控件中的记录
           for (int i = 0; i < dgv.RowCount - 1; i++)
           {
                for (int j = 0; j < dgv.ColumnCount; j++)
                {
                    mytable.Cell(i+2, j + 1).Range.InsertAfter(dgv[j, i].Value.ToString());
                }
           }     
            return true;
 }
  public void SaveDocument(string filePath)
  {
            object fileName = filePath;
            object format = WdSaveFormat.wdFormatDocument;//保存格式
            object miss = System.Reflection.Missing.Value;
            wordDoc.SaveAs(ref fileName, ref format, ref miss,
              ref miss, ref miss, ref miss, ref miss,
              ref miss, ref miss, ref miss, ref miss,
              ref miss, ref miss, ref miss, ref miss,
              ref miss);
            //输出完毕后关闭doc对象
            object IsSave = true;
            wordDoc.Close(ref IsSave, ref missing, ref missing);
            wordApp.Quit(ref missing, ref missing, ref missing);
 }
        //关闭当前文件
 public bool CloseDocument()
 {
         try
            {
                object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                wordDoc.Close(ref doNotSaveChanges, ref missing, ref missing);
                wordDoc = null;
                return true;
            }
            catch (Exception)
            {
                return false;
            }
 }
private void Button1_Click(object sender, EventArgs e)
{
            string tempPath = System.Windows.Forms.Application.StartupPath + "\\唯一.dot";
            //生成的具有模板样式的新文件
            string fileName = @"C:\Users\Administrator\Desktop\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
            myReport.ExportDataGridview(dataGridView1,true,tempPath, fileName);
            string reportname = richTextBox1.Text;
            string reportname1 = richTextBox2.Text;
            string reportname3 = Head.Text;
            string reportname4 = DateTime.Now.ToString("yyyy年-MM月-dd日");
            myReport.Insertxt("Pro1", reportname);
            myReport.Insertxt1("Pro2", reportname1);
            myReport.Insertxt3("Heads", reportname3);
            myReport.Insertxt4("Timard",reportname4);
 }


  • 写回答

2条回答 默认 最新

  • hello world 杂货铺 2021-12-09 22:44
    关注

    最好的方法是放一个报告模板之类的,然后把报告读到内存中,再把你的表格贴进去输出就行了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 已采纳回答 12月13日
  • 修改了问题 12月8日
  • 创建了问题 12月8日

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算