C#将DataTable海量数据导出到Excel

8 篇文章 0 订阅

将DataTable中的大量数据导出到Excel表格中。

但每张Excel表单只能有65536行,所以当DataTable数据多于65536行时,Excel要考虑分页功能。

 

通过查阅相关资料,得到导出方法。

代码在Office 2003 环境下通过。末验证其它Office版本。

[c-sharp]  view plain copy
  1. using Excel = Microsoft.Office.Interop.Excel;  
  2.   
  3.   
  4. public Excel.Application m_xlApp = null;  
  5.   
  6.   
  7. /// <summary>  
  8. /// 将DataTable数据导出到Excel表  
  9. /// </summary>  
  10. /// <param name="tmpDataTable">要导出的DataTable</param>  
  11. /// <param name="strFileName">Excel的保存路径及名称</param>  
  12. public void DataTabletoExcel(System.Data.DataTable tmpDataTable, string strFileName)  
  13. {  
  14.     if (tmpDataTable == null)  
  15.     {  
  16.         return;  
  17.     }  
  18.     long rowNum = tmpDataTable.Rows.Count;//行数  
  19.     int columnNum = tmpDataTable.Columns.Count;//列数  
  20.     Excel.Application m_xlApp = new Excel.Application();  
  21.     m_xlApp.DisplayAlerts = false;//不显示更改提示  
  22.     m_xlApp.Visible = false;  
  23.   
  24.     Excel.Workbooks workbooks = m_xlApp.Workbooks;  
  25.     Excel.Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);  
  26.     Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];//取得sheet1  
  27.   
  28.     try  
  29.     {  
  30.         if (rowNum > 65536)//单张Excel表格最大行数  
  31.         {  
  32.             long pageRows = 65535;//定义每页显示的行数,行数必须小于65536  
  33.             int scount = (int)(rowNum / pageRows);//导出数据生成的表单数  
  34.             if (scount * pageRows < rowNum)//当总行数不被pageRows整除时,经过四舍五入可能页数不准  
  35.             {  
  36.                 scount = scount + 1;  
  37.             }  
  38.             for (int sc = 1; sc <= scount; sc++)  
  39.             {  
  40.                 if (sc > 1)  
  41.                 {  
  42.                     object missing = System.Reflection.Missing.Value;  
  43.                     worksheet = (Excel.Worksheet)workbook.Worksheets.Add(  
  44.                                 missing, missing, missing, missing);//添加一个sheet  
  45.                 }  
  46.                 else  
  47.                 {  
  48.                     worksheet = (Excel.Worksheet)workbook.Worksheets[sc];//取得sheet1  
  49.                 }  
  50.                 string[,] datas = new string[pageRows + 1, columnNum];  
  51.   
  52.                 for (int i = 0; i < columnNum; i++) //写入字段  
  53.                 {  
  54.                     datas[0, i] = tmpDataTable.Columns[i].Caption;//表头信息  
  55.                 }  
  56.                 Excel.Range range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnNum]);  
  57.                 range.Interior.ColorIndex = 15;//15代表灰色  
  58.                 range.Font.Bold = true;  
  59.                 range.Font.Size = 9;  
  60.   
  61.                 int init = int.Parse(((sc - 1) * pageRows).ToString());  
  62.                 int r = 0;  
  63.                 int index = 0;  
  64.                 int result;  
  65.                 if (pageRows * sc >= rowNum)  
  66.                 {  
  67.                     result = (int)rowNum;  
  68.                 }  
  69.                 else  
  70.                 {  
  71.                     result = int.Parse((pageRows * sc).ToString());  
  72.                 }  
  73.   
  74.                 for (r = init; r < result; r++)  
  75.                 {  
  76.                     index = index + 1;  
  77.                     for (int i = 0; i < columnNum; i++)  
  78.                     {  
  79.                         object obj = tmpDataTable.Rows[r][tmpDataTable.Columns[i].ToString()];  
  80.                         datas[index, i] = obj == null ? "" : "'" + obj.ToString().Trim();//在obj.ToString()前加单引号是为了防止自动转化格式  
  81.                     }  
  82.                     System.Windows.Forms.Application.DoEvents();  
  83.                     //添加进度条  
  84.                 }  
  85.   
  86.                 Excel.Range fchR = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[index + 1, columnNum]);  
  87.                 fchR.Value2 = datas;  
  88.                 worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。  
  89.                 m_xlApp.WindowState = Excel.XlWindowState.xlMaximized;//Sheet表最大化  
  90.                 range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[index + 1, columnNum]);  
  91.                 //range.Interior.ColorIndex = 15;//15代表灰色  
  92.                 range.Font.Size = 9;  
  93.                 range.RowHeight = 14.25;  
  94.                 range.Borders.LineStyle = 1;  
  95.                 range.HorizontalAlignment = 1;  
  96.             }  
  97.         }  
  98.         else  
  99.         {  
  100.             string[,] datas = new string[rowNum + 1, columnNum];  
  101.             for (int i = 0; i < columnNum; i++) //写入字段  
  102.             {  
  103.                 datas[0, i] = tmpDataTable.Columns[i].Caption;  
  104.             }  
  105.             Excel.Range range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[1, columnNum]);  
  106.             range.Interior.ColorIndex = 15;//15代表灰色  
  107.             range.Font.Bold = true;  
  108.             range.Font.Size = 9;  
  109.   
  110.             int r = 0;  
  111.             for (r = 0; r < rowNum; r++)  
  112.             {  
  113.                 for (int i = 0; i < columnNum; i++)  
  114.                 {  
  115.                     object obj = tmpDataTable.Rows[r][tmpDataTable.Columns[i].ToString()];  
  116.                     datas[r + 1, i] = obj == null ? "" : "'" + obj.ToString().Trim();//在obj.ToString()前加单引号是为了防止自动转化格式  
  117.                 }  
  118.                 System.Windows.Forms.Application.DoEvents();  
  119.                 //添加进度条  
  120.             }  
  121.             Excel.Range fchR = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]);  
  122.             fchR.Value2 = datas;  
  123.   
  124.             worksheet.Columns.EntireColumn.AutoFit();//列宽自适应。  
  125.             m_xlApp.WindowState = Excel.XlWindowState.xlMaximized;  
  126.   
  127.             range = worksheet.get_Range(worksheet.Cells[1, 1], worksheet.Cells[rowNum + 1, columnNum]);  
  128.             //range.Interior.ColorIndex = 15;//15代表灰色  
  129.             range.Font.Size = 9;  
  130.             range.RowHeight = 14.25;  
  131.             range.Borders.LineStyle = 1;  
  132.             range.HorizontalAlignment = 1;  
  133.         }  
  134.         workbook.Saved = true;  
  135.         workbook.SaveCopyAs(strFileName);  
  136.     }  
  137.     catch (Exception ex)  
  138.     {  
  139.         MessageBox.Show("导出异常:" + ex.Message, "导出异常", MessageBoxButtons.OK, MessageBoxIcon.Warning);  
  140.     }  
  141.     finally  
  142.     {  
  143.         EndReport();  
  144.     }  
  145. }  
  146.   
  147.   
  148. /// <summary>  
  149. /// 退出报表时关闭Excel和清理垃圾Excel进程  
  150. /// </summary>  
  151. private void EndReport()  
  152. {  
  153.     object missing = System.Reflection.Missing.Value;  
  154.     try  
  155.     {  
  156.         m_xlApp.Workbooks.Close();  
  157.         m_xlApp.Workbooks.Application.Quit();  
  158.         m_xlApp.Application.Quit();  
  159.         m_xlApp.Quit();  
  160.     }  
  161.     catch { }  
  162.     finally  
  163.     {  
  164.         try  
  165.         {  
  166.             System.Runtime.InteropServices.Marshal.ReleaseComObject(m_xlApp.Workbooks);  
  167.             System.Runtime.InteropServices.Marshal.ReleaseComObject(m_xlApp.Application);  
  168.             System.Runtime.InteropServices.Marshal.ReleaseComObject(m_xlApp);  
  169.             m_xlApp = null;  
  170.         }  
  171.         catch { }  
  172.         try  
  173.         {  
  174.             //清理垃圾进程  
  175.             this.killProcessThread();  
  176.         }  
  177.         catch { }  
  178.         GC.Collect();  
  179.     }  
  180. }  
  181. /// <summary>  
  182. /// 杀掉不死进程  
  183. /// </summary>  
  184. private void killProcessThread()  
  185. {  
  186.     ArrayList myProcess = new ArrayList();  
  187.     for (int i = 0; i < myProcess.Count; i++)  
  188.     {  
  189.         try  
  190.         {  
  191.             System.Diagnostics.Process.GetProcessById(int.Parse((string)myProcess[i])).Kill();  
  192.         }  
  193.         catch { }  
  194.     }  
  195. }

相关资料:

1. http://www.cnblogs.com/Ihaveadream/archive/2009/01/04/1368525.html

2. http://msdn.microsoft.com/zh-cn/library/aa168292(office.11).aspx

3. http://msdn.microsoft.com/zh-cn/library/microsoft.office.tools.excel(VS.80).aspx









  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值