/// <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. Django之分页显示文章

    1.项目:http://www.cnblogs.com/jasonhaven/p/7493422.html 2.任务描述:页面分页显示文章 3.源代码 后台: from django.core.pag ...

  2. html5-4 HTML5超链接、URL地址和表格

    html5-4 HTML5超链接.URL地址和表格 一.总结 一句话总结: 1.cellspace有什么用? 清除表格的单元格间距 26 <table border='1px' cellspac ...

  3. [Ramda] Refactor to Point Free Functions with Ramda using compose and converge

    In this lesson we'll take some existing code and refactor it using some functions from the Ramda lib ...

  4. Method and apparatus for loading a segment register in a microprocessor capable of operating in multiple modes

    A microprocessor contains an address generation unit, including a segment block, for loading descrip ...

  5. php课程 6-22 字符串格式化函数有哪些(精问)

    php课程 6-22 字符串格式化函数有哪些(精问) 一.总结 一句话总结: 1.猜测一下$_GET()怎么来的? 函数赋值给变量的操作:$_YZM=get();   这样就可以很好的解释哪些全局变量 ...

  6. MAC 安装下载好的.gz包(不像.dmg直接双击就行了)

    下载之后解压,然后在terminal里敲命令吧: 复制文件:蓝色部分为你解压文件的路径,直接拖到这里就行了$ sudo mv mysql-5.1.45-osx10.6-x86_64文件路径 /usr/ ...

  7. Android分享介绍

    一.使用系统分享 public void execShare(Activity context,String title,String text){ Intent intent = new Inten ...

  8. Array方法总结

    一.不影响原数组产生一个新数组 slice:切片->返回新数组->复制数组:arr.slice(0) arrayObject.slice(start,end): 切片 var arr= [ ...

  9. 1.在windows下安装rabbitMQ

    a .RabbitMQ是用erLang语言写的,所以我们在安装rabbitMQ之前要先安装erLang. 要安装最新版本的请分别前往 www.erlang.org和www.rabbitmq.com网站 ...

  10. 【003】【Java虚拟机——对象死亡的判断】

    对象死亡! 垃圾收集器在对堆进行回收前,首先要做的事情就是要确定这些对象之中哪些还"存活"着, 哪些已经"死去" (即不可能再被不论什么途径使用的对象). 1)  引用计 ...