热线电话:13121318867

登录
首页精彩阅读NPOI大数据分批写入同个Excel
NPOI大数据分批写入同个Excel
2016-02-27
收藏

NPOI大数据分批写入同个Excel

要导出来的数据库数据量很大,一次取出来压力有点大,故分批取出来,导入到同一个Excel。
因为Excel2003版最大行数是65536行,Excel2007开始的版本最大行数是1048576行,故NPOI导出时候选择了Excel2007。

Form1.cs


[csharp] view plaincopyPRint?
  1. /* 
  2. 引用命名空间: 
  3. using System.IO; 
  4. using System.Threading.Tasks; 
  5. using NPOI.HSSF.UserModel; 
  6. using NPOI.SS.UserModel; 
  7. using NPOI.XSSF.UserModel; 
  8. */  
  9.         
  10. public Form1()  
  11. {  
  12.     InitializeComponent();  
  13.     List<DictionaryEntry> list = new List<DictionaryEntry>(){  
  14.           new DictionaryEntry(1, "XA"),  
  15.           new DictionaryEntry(2, "XB")  
  16.     };  
  17.     cbType.BindComboBox(list);                      
  18. }  
  19.   
  20. private void CreateExcel(string fileName)  
  21. {  
  22.     if (File.Exists(fileName))  
  23.         File.Delete(fileName);  
  24.   
  25.     IWorkbook workbook = new XSSFWorkbook();  
  26.     ISheet sheet = workbook.CreateSheet("Sheet1");  
  27.     FileStream sw = File.Create(fileName);  
  28.     workbook.Write(sw);  
  29.     sw.Close();                     
  30. }  
  31.   
  32.   
  33. private void btnExport_Click(object sender, EventArgs e)  
  34. {              
  35.     try  
  36.     {  
  37.         Task.Factory.StartNew(() =>  
  38.         {  
  39.             txtSql.SafeCall(() =>  
  40.             {  
  41.                 txtSql.AppendText("开始处理...\r\n");  
  42.             });  
  43.   
  44.             BusinessType businessType = GetBusinessType();  
  45.             string[] sqlWhereArray = Sql.SqlWhereArray;  
  46.             string[] DateRemarkArray = Sql.DateRemarkArray;  
  47.             string fileName = string.Format("{0}.xlsx", businessType.ToString());  
  48.   
  49.             CreateExcel(fileName);                                         
  50.   
  51.             string sqlCount = Sql.GetRecordSql(businessType, "");  
  52.             int recordCount = db.ExecuteScalar(sqlCount);     
  53.             int sqlIndex = 0;  
  54.             int rowIndex = 0;  
  55.             foreach (string sqlWhre in sqlWhereArray)                      
  56.             {  
  57.                 sqlIndex++;  
  58.                 FileStream fs = File.Open(fileName, FileMode.Open);    
  59.                 IWorkbook workbook = new XSSFWorkbook(fs);                        
  60.                 ISheet sheet = workbook.GetSheetAt(0);  
  61.                 txtSql.SafeCall(() =>  
  62.                 {  
  63.                     txtSql.AppendText("条件" + sqlIndex.ToString() + ":" + DateRemarkArray[sqlIndex - 1]);  
  64.                 });  
  65.                 string sql = Sql.GetDataSql(businessType, sqlWhre);  
  66.                 DataTable dt = db.GetDataSet(sql).Tables[0];  
  67.   
  68.                 int columnsCount = dt.Columns.Count;  
  69.                 if (sqlIndex == 1)  
  70.                 {  
  71.                     IRow row0 = sheet.CreateRow(0);                              
  72.                     for (int m = 0; m < columnsCount; m++)  
  73.                     {  
  74.                         DataColumn dc = dt.Columns[m];  
  75.                         row0.CreateCell(m).SetCellValue(dc.ColumnName);  
  76.                     }  
  77.                 }  
  78.   
  79.                 for (int i = 0; i < dt.Rows.Count; i++)  
  80.                 {  
  81.                     rowIndex++;  
  82.                     DataRow dr = dt.Rows[i];                          
  83.                     IRow row = sheet.CreateRow(rowIndex);  
  84.                     for (int j = 0; j < columnsCount; j++)  
  85.                     {  
  86.                         row.CreateCell(j).SetCellValue(dr[j].ToString());  
  87.                     }  
  88.   
  89.                     lblMsg.SafeCall(() =>  
  90.                     {  
  91.                         if(i == (dt.Rows.Count - 1))  
  92.                             txtSql.AppendText(" 行数:" + (i+1).ToString() + "\r\n");  
  93.                         lblMsg.Text = string.Format("正在导出第{0}个条件,第{1}行", sqlIndex.ToString(), (i + 1).ToString());  
  94.                         double x = rowIndex * 1.0 / recordCount * 100;  
  95.                         lblProgress.Text = string.Format("总行数:{0}, 当前完成总{1}行,百分占比:{2} %", recordCount.ToString(), rowIndex.ToString(), x.ToString("#0.0"));  
  96.                     });  
  97.                 }  
  98.                 FileStream outFs = new FileStream(fileName, FileMode.Open);  
  99.                 workbook.Write(outFs);  
  100.                 outFs.Close();  
  101.             }                     
  102.         }).ContinueWith(TaskEnded);  
  103.     }  
  104.     catch (Exception ex)  
  105.     {  
  106.         MessageBox.Show("发生异常,错误提示:" + ex.Message);  
  107.     }  
  108. }  
  109. private void TaskEnded(Task task)  
  110. {  
  111.     txtSql.SafeCall(() =>  
  112.     {  
  113.         lblMsg.Text = "全部导出完成!";  
  114.         txtSql.AppendText("处理完成!\r\n");                 
  115.     });  
  116. }  


Extensions.cs


[csharp] view plaincopyprint?
 
  1. public static class Extensions  
  2. {  
  3.     public static void SafeCall(this Control ctrl, Action callback)  
  4.     {  
  5.         if (ctrl.InvokeRequired)  
  6.             ctrl.Invoke(callback);  
  7.         else  
  8.             callback();  
  9.     }  
  10.     public static void BindComboBox(this ComboBox cb, List<DictionaryEntry> list)  
  11.     {  
  12.         cb.DisplayMember = "Value";  
  13.         cb.ValueMember = "Key";  
  14.         cb.DataSource = list;  
  15.     }  
  16. }  

Sql.cs



[csharp] view plaincopyprint?
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace DataExport  
  7. {  
  8.     public enum BusinessType  
  9.     {  
  10.         XA = 1,  
  11.         XB = 2  
  12.     }  
  13.   
  14.     public class Sql  
  15.     {  
  16.         /// <summary>  
  17.         /// 分批获取sql的where条件  
  18.         /// </summary>  
  19.         public static string[] SqlWhereArray = {   
  20.                                  " 条件1 ",  
  21.                                  " 条件2 ",  
  22.                                  " 条件3 "  
  23.                                  };  
  24.           
  25.         /// <summary>  
  26.         /// sql的where条件说明  
  27.         /// </summary>  
  28.         public static string[] DateRemarkArray = {    
  29.                             "20130101至20130331",  
  30.                             "20130401至20130630",  
  31.                 "20130701后",  
  32.                              };  
  33.   
  34.         /// <summary>  
  35.         /// 获取sql语句  
  36.         /// </summary>  
  37.         /// <param name="type"></param>  
  38.         /// <param name="columns"></param>  
  39.         /// <param name="sqlWhere"></param>  
  40.         /// <returns></returns>  
  41.         private static string GetSql(BusinessType type, string columns, string sqlWhere)  
  42.         {  
  43.             string sql = "";  
  44.             switch (type)  
  45.             {  
  46.                 case BusinessType.XA:  
  47.                     sql = string.Format(@"SELECT {0}  FROMM tb1 WHERE 1=1 {1} ", columns, sqlWhere);  
  48.                     break;  
  49.                 case BusinessType.XB:  
  50.                     sql = string.Format(@"SELECT {0}  FROMM tb2 WHERE 1=1 {1} ", columns, sqlWhere);  
  51.                     break;  
  52.             }  
  53.   
  54.   
  55.             return sql;  
  56.         }  
  57.   
  58.         /// <summary>  
  59.         /// 获取总记录数  
  60.         /// </summary>  
  61.         /// <param name="type"></param>  
  62.         /// <param name="sqlWhere"></param>  
  63.         /// <returns></returns>  
  64.         public static string GetRecordSql(BusinessType type, string sqlWhere)  
  65.         {  
  66.             string columns = "count(*)";  
  67.             return GetSql(type, columns, sqlWhere);  
  68.         }  
  69.   
  70.         /// <summary>  
  71.         /// 获取数据  
  72.         /// </summary>  
  73.         /// <param name="type"></param>  
  74.         /// <param name="sqlWhere"></param>  
  75.         /// <returns></returns>  
  76.         public static string GetDataSql(BusinessType type, string sqlWhere)  
  77.         {  
  78.             string columns = "";  
  79.             switch (type)  
  80.             {  
  81.                 case BusinessType.XA:  
  82.                                 columns = @"  
  83.                                 col1 列1,  
  84.                 col2 列2,  
  85.                                 col3 列3  
  86.                                  ";  
  87.                     break;  
  88.                 case BusinessType.XB:  
  89.                                 columns = @"  
  90.                                 col1 列1,  
  91.                 col2 列2  
  92.                                  ";  
  93.                     break;  
  94.                  
  95.                     }  
  96.             return GetSql(type, columns, sqlWhere);  
  97.        }  
  98.     }  
  99. }  


备注:

NPOI组件下载:http://download.csdn.net/detail/gdjlc/5820023


=========================================================

另外,NPOI只有 2.0以上版本才支持Excel 2007,用法

//自动辨别2007或2003
IWorkbook workbook = WorkbookFactory.Create(FileUpload1.FileContent);
//2007
XSSFWorkbook workbook = new XSSFWorkbook(FileUpload1.FileContent);
//2003
HSSFWorkbook workbook = new HSSFWorkbook(FileUpload1.FileContent);

数据分析咨询请扫描二维码

若不方便扫码,搜微信号:CDAshujufenxi

最新资讯
更多
客服在线
立即咨询