• c#通过datatable导出excel和word


     /// <summary>
           /// 导出datatable到word
           /// </summary>
           /// <param name="dg">需要中文列名的表</param>
           public static void ExportWord(DataTable dg)
           {
               try
               {
                   if (dg.Rows.Count != 0)
                   {
                       //建表格
                       object Nothing = System.Reflection.Missing.Value;
                       Word._Application oWord;
                       Word._Document oDoc;
                       oWord = new Word.Application();
                       oWord.Visible = true;
                       oDoc = oWord.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                       object start = 0;
                       object end = 0;
                       Word.Range tableLocation = oDoc.Range(ref start, ref end);
                       oDoc.Tables.Add(tableLocation, dg.Rows.Count+1, dg.Columns.Count, ref Nothing, ref Nothing);
                       Word.Table newTable = oDoc.Tables[1];
                       newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                       newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
                       object beforeRow = newTable.Rows[1];
                       newTable.Rows.Add(ref beforeRow);
    
                       //赋值
                       for(int i=0;i<dg.Columns.Count;i++)
                       {
                           newTable.Cell(1, i+1).Range.Text = dg.Columns[i].ColumnName;
                       }
                       for (int i = 0; i < dg.Rows.Count; i++)
                       {
                           for (int j = 0; j < dg.Columns.Count; j++)
                           {
                               newTable.Cell(i + 2, j+1).Range.Text = dg.Rows[i][j].ToString();
                           }
                       }
    
                       object fileName = "";
                       oDoc.SaveAs(fileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
                            ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                       oWord.Documents.Open(fileName, Nothing, false, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing);
                   }
               }
               catch
               { }
           
    
    /// <summary>
           /// 导出datatable到Excel
           /// </summary>
           /// <param name="dg">需要中文列名的表</param>
           public static void ExportExcel(DataTable dg)
           {
               try
               {
                   if (dg.Rows.Count != 0)
                   {
                       Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                       //Microsoft.Office.Interop.Excel.ApplicationClass ac = new Microsoft.Office.Interop.Excel.ApplicationClass();
                    
                       Microsoft.Office.Interop.Excel.Workbook wb; //这里千万不能使用 new 来实例对象,不然会异常
    
                       Microsoft.Office.Interop.Excel.Worksheet ws;
    
    
    
                    
                       wb = app.Workbooks.Add(System.Reflection.Missing.Value);  //创建工作簿(WorkBook:即Excel文件主体本身)
                       ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);        //创建工作表(Worksheet:工作表,即Excel里的子表sheet)
    
                       //设置表名
                       ws.Name = dg.TableName == "" ? "Table" : dg.TableName;
                       //赋值
                       for (int i = 0; i < dg.Columns.Count; i++)
                       {
                           ws.Cells[1, i + 1] = dg.Columns[i].ColumnName;
                       }
                       //将数据导入到工作表的单元格
                       for (int i = 0; i < dg.Rows.Count; i++)
                       {
                           for (int j = 0; j < dg.Columns.Count; j++)
                               ws.Cells[i + 2, j + 1] = dg.Rows[i][j].ToString();
                       }
                       string strPath = Environment.CurrentDirectory;
                       Microsoft.Win32.SaveFileDialog dialogOpenFile = new Microsoft.Win32.SaveFileDialog();
                       dialogOpenFile.DefaultExt = "xls";//默认扩展名
                       dialogOpenFile.AddExtension = true;//是否自动添加扩展名
                       dialogOpenFile.Filter = "*.xls|.xls";
                       dialogOpenFile.OverwritePrompt = true;//文件已存在是否提示覆盖
                       dialogOpenFile.FileName = ws.Name;//默认文件名
                       dialogOpenFile.CheckPathExists = true;//提示输入的文件名无效
                       dialogOpenFile.Title = "保存EXCEL";
                       //显示对话框
                       bool? b = dialogOpenFile.ShowDialog();
                       if (b == true)//点击保存
                       {
                           //保存到文件
                           wb.SaveAs(dialogOpenFile.FileName, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                       }
                       
                       //恢复系统路径-涉及不到的可以去掉
                       Environment.CurrentDirectory = strPath;
                      
                       //关闭
                       wb.Close(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
                      
                   }
               }
               catch
               { }
    
           }
  • 相关阅读:
    启动程序相关的命令
    分享的几行代码
    各种大数据软件安装
    tomcat报没法分配内存大小解决方案
    数据库事务
    pytorch之CNN实现
    搜索与匹配
    调试 pytorch 及 python 的 特殊语法
    图神经网络 GCN 等综述(转载)
    关于【finder不能完成该操作 因为未能读取或写入"文件名"中的某些数据(错误代码-36)】(实测,好用)
  • 原文地址:https://www.cnblogs.com/chuhaida/p/3913940.html
Copyright © 2020-2023  润新知