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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探