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 ...
随机推荐
- 跟我学AngularJs:Service、Factory、Provider依赖注入使用与差别
林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 本教程使用AngularJs版本号:1.5.3 AngularJ ...
- php 获取提交来源,判断从哪里提交的
echo $_SERVER['HTTP_REFERER'];这个获取上个页面的url例如获得的是 $url = http://www.weisuyun.com/nihao.html其他页面提交过来的不 ...
- 【前端统计图】hcharts实现堆叠柱形图(与后台数据交互)
原型图类似如下: 图片.png <!DOCTYPE > <html> <head> <meta charset="utf-8">&l ...
- sql for xml 还有一种写法(採用 tag 与 union all,简洁易懂)
sql for xml 还有一种写法(採用 tag 与 union all,简洁易懂) 測试环境:sql 08, 08 R2, 2010, 2012, 2014 等 declare @agent t ...
- [React] Use React ref to Get a Reference to Specific Components
When you are using React components you need to be able to access specific references to individual ...
- layer弹框在实际项目中的一些应用
官方介绍:layer至今仍作为layui的代表作,受众广泛并非偶然,而是这五年多的坚持,不断完善和维护.不断建设和提升社区服务,使得猿们纷纷自发传播,乃至于成为今天的Layui最强劲的源动力.目前,l ...
- OpenCV中CvSVM部分函数解读
CvSVM::predict函数解析:无论是Mat接口还是CvMat接口终于都是通过指针的形式调用的.也就是终于都是调用的下面函数实现的 float CvSVM::predict( const flo ...
- 【25.00%】【codeforces 584E】Anton and Ira
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Objective-C 中的Runtime的使用
Runtime的使用 一直以来,OC被大家冠以动态语言的称谓,其实是因为OC中包含的runtime机制.Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平 ...
- JAVA SE回顾及思考(1)——面向对象的特点
学习Java已经三年了,现在开始做Android开发,虽说还在用Java语言但本人现在才真真的意识到无论学什么基础才是最重要的,可能一些刚接触Java或者Android的朋友现在还体会不到基础的重要性 ...