/// <summary>
/// 导出datatable到word
/// </summary>
/// <param name="dg">需要中文列名的表</param>
public static void ExportWord(DataTable dg)
{
try
{
if (dg.Rows.Count != )
{
//建表格
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 = ;
object end = ;
Word.Range tableLocation = oDoc.Range(ref start, ref end);
oDoc.Tables.Add(tableLocation, dg.Rows.Count+, dg.Columns.Count, ref Nothing, ref Nothing);
Word.Table newTable = oDoc.Tables[];
newTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
newTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleSingle;
object beforeRow = newTable.Rows[];
newTable.Rows.Add(ref beforeRow); //赋值
for(int i=;i<dg.Columns.Count;i++)
{
newTable.Cell(, i+).Range.Text = dg.Columns[i].ColumnName;
}
for (int i = ; i < dg.Rows.Count; i++)
{
for (int j = ; j < dg.Columns.Count; j++)
{
newTable.Cell(i + , j+).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 != )
{
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 = ; i < dg.Columns.Count; i++)
{
ws.Cells[, i + ] = dg.Columns[i].ColumnName;
}
//将数据导入到工作表的单元格
for (int i = ; i < dg.Rows.Count; i++)
{
for (int j = ; j < dg.Columns.Count; j++)
ws.Cells[i + , j + ] = 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
{ } }

c#通过datatable导出excel和word的更多相关文章

  1. asp.net DataTable导出Excel 自定义列名

    1.添加引用NPOI.dll 2.cs文件头部添加 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; 3.代码如 ...

  2. Datatable导出Excel

    ; IRow headerRow = sheet.CreateRow(); ; ; ; iRowIndex++; } ; i < icolIndex; i++) { sheet.AutoSize ...

  3. c# Datatable导出Excel

    using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; ...

  4. C# DataTable导出EXCEL后身份证、银行卡号等长数字信息显示乱码解决

    在DataTable导出EXCEL后发现有些格式显示有问题,比如身份证.银行卡号等大于11位的数字显示为科学计数法.13681-1等 带中划线的两段数字显示为日期格式等. 处理方法如下: public ...

  5. RDLC - 后台代码直接导出Excel/PDF/Word格式

    最近做报表功能,用到了.net的报表组件rdlc. 其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,网上看了些资源,做个总结: 参考地址 我直接贴出代码: //自动导出exce ...

  6. 【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码

    本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进 ...

  7. EasyOffice-.NetCore一行代码导入导出Excel,生成Word

    简介 Excel和Word操作在开发过程中经常需要使用,这类工作不涉及到核心业务,但又往往不可缺少.以往的开发方式在业务代码中直接引入NPOI.Aspose或者其他第三方库,工作繁琐,耗时多,扩展性差 ...

  8. 导出excel、word、csv文件方法汇总

    http://www.woaic.com/2012/06/64 excel文件主要是输出html代码.以xls的文本格式保存文件. 生成excel格式的代码: /// <summary> ...

  9. DataTable 更改在有数据列的类型方法+DataTable 导出excel功能

    /// <summary> /// 导出功能 /// </summary> /// <param name="sender"></para ...

随机推荐

  1. Android中的动画详解系列【1】——逐帧动画

    逐帧动画其实很简单,下面我们来看一个例子: <?xml version="1.0" encoding="utf-8"?> <animation ...

  2. [Grid Layout] Place grid items on a grid using grid-column and grid-row

    It’s possible to position a grid item anywhere on a grid track. To do this, let’s specify some grid- ...

  3. 学汇编的时候可以拿IDA之类的反汇编工具辅助学习,再用gdb或者IDA动态调试,跟踪每条指令的 执行结果。都不难

    作者:潘安仁链接:https://www.zhihu.com/question/40720890/answer/87926792来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  4. [Postgre] Insert Data into Postgre Tables

    // Insert one row INSERT INTO movies (title, release_date, count_stars, director_id) VALUES ( 'Kill ...

  5. [NPM] Use a shorthand syntax for running multiple npm scripts with npm-run-all

    Running multiple scripts in series or in parallel can become very verbose. Using a tool such as npm- ...

  6. uva 1519 - Dictionary Size(字典树)

    题目链接:uva 1519 - Dictionary Size 题目大意:给出n个字符串组成的字典.如今要加入新的单词,从已有单词中选出非空前缀和非空后缀,组成新单词. 问说能组成多少个单词. 解题思 ...

  7. Codeforces Round #443 (Div. 2) C: Short Program - 位运算

    传送门 题目大意: 输入给出一串位运算,输出一个步数小于等于5的方案,正确即可,不唯一. 题目分析: 英文题的理解真的是各种误差,从头到尾都以为解是唯一的. 根据位运算的性质可以知道: 一连串的位运算 ...

  8. [Android]使用化名(alias)功能防止相同资源的重复

    在为一个应用匹配不同资源文件的时候,有时可能需要在不同适配类型的资源路径下使用相同的资源文件,这时使用alias方法可以防止相同资源文件的重复,提高效率.以下摘自Android开发文档http://d ...

  9. matlab 中使用 GPU 加速运算

    为了提高大规模数据处理的能力,matlab 的 GPU 并行计算,本质上是在 cuda 的基础上开发的 wrapper,也就是说 matlab 目前只支持 NVIDIA 的显卡. 1. GPU 硬件支 ...

  10. .NET CORE的TagHelper智能提示

    VisualStudio2017下ASP.NET CORE的TagHelper智能提示不能使用的解决办法   之前在VS2017RC中就发现该问题,安装了依赖,但是前段一直点不出来asp-for,后来 ...