c#通过datatable导出excel和word
/// <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的更多相关文章
- asp.net DataTable导出Excel 自定义列名
1.添加引用NPOI.dll 2.cs文件头部添加 using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using System.IO; 3.代码如 ...
- Datatable导出Excel
; IRow headerRow = sheet.CreateRow(); ; ; ; iRowIndex++; } ; i < icolIndex; i++) { sheet.AutoSize ...
- c# Datatable导出Excel
using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; ...
- C# DataTable导出EXCEL后身份证、银行卡号等长数字信息显示乱码解决
在DataTable导出EXCEL后发现有些格式显示有问题,比如身份证.银行卡号等大于11位的数字显示为科学计数法.13681-1等 带中划线的两段数字显示为日期格式等. 处理方法如下: public ...
- RDLC - 后台代码直接导出Excel/PDF/Word格式
最近做报表功能,用到了.net的报表组件rdlc. 其中有个功能就是后台代码直接输出Excel/PDF/Word格式的文件,网上看了些资源,做个总结: 参考地址 我直接贴出代码: //自动导出exce ...
- 【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码
本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进 ...
- EasyOffice-.NetCore一行代码导入导出Excel,生成Word
简介 Excel和Word操作在开发过程中经常需要使用,这类工作不涉及到核心业务,但又往往不可缺少.以往的开发方式在业务代码中直接引入NPOI.Aspose或者其他第三方库,工作繁琐,耗时多,扩展性差 ...
- 导出excel、word、csv文件方法汇总
http://www.woaic.com/2012/06/64 excel文件主要是输出html代码.以xls的文本格式保存文件. 生成excel格式的代码: /// <summary> ...
- DataTable 更改在有数据列的类型方法+DataTable 导出excel功能
/// <summary> /// 导出功能 /// </summary> /// <param name="sender"></para ...
随机推荐
- C++中回调(CallBack)的使用方法(其实就是类方法指针,我觉得你的方法易用性不好,虽然原理正确)
回调函数是一个很有用,也很重要的概念.当发生某种事件时,系统或其他函数将会自动调用你定义的一段函数.回调函数在windows编程使用的场合很多,比如Hook回调函数:MouseProc,GetMsgP ...
- Django日志器的使用
Logging Mudel A quick logging primer Django uses Python’s builtin logging module to perform system l ...
- mysql zip文件安装
bin目录下执行mysqld -install再执行mysqld --initialize-insecure 启动服务:net start mysql
- [Angular] Create dynamic content with <tempalte>
To create a dynamic template, we need to a entry component as a placeholder. Then we can use entry c ...
- 【hdu 3537】Daizhenyang's Coin
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- WPF之神奇的资源
原文:WPF之神奇的资源 WPF中的资源有两种,一种称为"程序集资源"(assembly resource),另一种称为"对象资源"(object resour ...
- 善用Linux与Windows中的筛选功能及其他有用功能
cmd中的检索目录结构是用 tree命令,检索本目录中这一级别的所有文件是dir,要是文件很多时需要用到检索功能 dir | find "abc" #####主要find之后要加双 ...
- T-SQL部分函数(转)
函数类别 作用 聚合函数 执行的操作是将多个值合并为一个值.例如 COUNT.SUM.MIN 和 MAX. 配置函数 是一种标量函数,可返回有关配置设置的信息. 转换函数 将值从一种数据类型转换为另一 ...
- oracle 全部查询和表空间,以及其关系
select * from dba_users; 查看数据库里面全部用户,前提是你是有dba权限的帐号.如sys,system select * from all_users; 查看你能管 ...
- 【非常高%】【codeforces 733A】Grasshopper And the String
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...