博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
NPOI大数据分批写入同个Excel
阅读量:6752 次
发布时间:2019-06-25

本文共 8212 字,大约阅读时间需要 27 分钟。

实现过程:

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

Form1.cs

/*     引用命名空间:     using System.IO;     using System.Threading.Tasks;     using NPOI.HSSF.UserModel;     using NPOI.SS.UserModel;     using NPOI.XSSF.UserModel;     */                  public Form1()      {          InitializeComponent();          List
list = new List
(){ new DictionaryEntry(1, "XA"), new DictionaryEntry(2, "XB") }; cbType.BindComboBox(list); } private void CreateExcel(string fileName) { if (File.Exists(fileName)) File.Delete(fileName); IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("Sheet1"); FileStream sw = File.Create(fileName); workbook.Write(sw); sw.Close(); } private void btnExport_Click(object sender, EventArgs e) { try { Task.Factory.StartNew(() => { txtSql.SafeCall(() => { txtSql.AppendText("开始处理...\r\n"); }); BusinessType businessType = GetBusinessType(); string[] sqlWhereArray = Sql.SqlWhereArray; string[] DateRemarkArray = Sql.DateRemarkArray; string fileName = string.Format("{0}.xlsx", businessType.ToString()); CreateExcel(fileName); string sqlCount = Sql.GetRecordSql(businessType, ""); int recordCount = db.ExecuteScalar(sqlCount); int sqlIndex = 0; int rowIndex = 0; foreach (string sqlWhre in sqlWhereArray) { sqlIndex++; FileStream fs = File.Open(fileName, FileMode.Open); IWorkbook workbook = new XSSFWorkbook(fs); ISheet sheet = workbook.GetSheetAt(0); txtSql.SafeCall(() => { txtSql.AppendText("条件" + sqlIndex.ToString() + ":" + DateRemarkArray[sqlIndex - 1]); }); string sql = Sql.GetDataSql(businessType, sqlWhre); DataTable dt = db.GetDataSet(sql).Tables[0]; int columnsCount = dt.Columns.Count; if (sqlIndex == 1) { IRow row0 = sheet.CreateRow(0); for (int m = 0; m < columnsCount; m++) { DataColumn dc = dt.Columns[m]; row0.CreateCell(m).SetCellValue(dc.ColumnName); } } for (int i = 0; i < dt.Rows.Count; i++) { rowIndex++; DataRow dr = dt.Rows[i]; IRow row = sheet.CreateRow(rowIndex); for (int j = 0; j < columnsCount; j++) { row.CreateCell(j).SetCellValue(dr[j].ToString()); } lblMsg.SafeCall(() => { if(i == (dt.Rows.Count - 1)) txtSql.AppendText(" 行数:" + (i+1).ToString() + "\r\n"); lblMsg.Text = string.Format("正在导出第{0}个条件,第{1}行", sqlIndex.ToString(), (i + 1).ToString()); double x = rowIndex * 1.0 / recordCount * 100; lblProgress.Text = string.Format("总行数:{0}, 当前完成总{1}行,百分占比:{2} %", recordCount.ToString(), rowIndex.ToString(), x.ToString("#0.0")); }); } FileStream outFs = new FileStream(fileName, FileMode.Open); workbook.Write(outFs); outFs.Close(); } }).ContinueWith(TaskEnded); } catch (Exception ex) { MessageBox.Show("发生异常,错误提示:" + ex.Message); } } private void TaskEnded(Task task) { txtSql.SafeCall(() => { lblMsg.Text = "全部导出完成!"; txtSql.AppendText("处理完成!\r\n"); }); }

 Extensions.cs

public static class Extensions      {          public static void SafeCall(this Control ctrl, Action callback)          {              if (ctrl.InvokeRequired)                  ctrl.Invoke(callback);              else                  callback();          }          public static void BindComboBox(this ComboBox cb, List
list) { cb.DisplayMember = "Value"; cb.ValueMember = "Key"; cb.DataSource = list; } }

 Sql.cs

using System;      using System.Collections.Generic;      using System.Linq;      using System.Text;            namespace DataExport      {          public enum BusinessType          {              XA = 1,              XB = 2          }                public class Sql          {              ///               /// 分批获取sql的where条件              ///               public static string[] SqlWhereArray = {                                        " 条件1 ",                                       " 条件2 ",                                       " 条件3 "                                       };                            ///               /// sql的where条件说明              ///               public static string[] DateRemarkArray = {                                    "20130101至20130331",                                  "20130401至20130630",                      "20130701后",                                   };                    ///               /// 获取sql语句              ///               ///               ///               ///               /// 
private static string GetSql(BusinessType type, string columns, string sqlWhere) { string sql = ""; switch (type) { case BusinessType.XA: sql = string.Format(@"SELECT {0} FROMM tb1 WHERE 1=1 {1} ", columns, sqlWhere); break; case BusinessType.XB: sql = string.Format(@"SELECT {0} FROMM tb2 WHERE 1=1 {1} ", columns, sqlWhere); break; } return sql; } /// /// 获取总记录数 /// /// /// ///
public static string GetRecordSql(BusinessType type, string sqlWhere) { string columns = "count(*)"; return GetSql(type, columns, sqlWhere); } /// /// 获取数据 /// /// /// ///
public static string GetDataSql(BusinessType type, string sqlWhere) { string columns = ""; switch (type) { case BusinessType.XA: columns = @" col1 列1, col2 列2, col3 列3 "; break; case BusinessType.XB: columns = @" col1 列1, col2 列2 "; break; } return GetSql(type, columns, sqlWhere); } } }

 

 

 

 

 

转载地址:http://dfvho.baihongyu.com/

你可能感兴趣的文章
RAID详解
查看>>
Java NIO:NIO概述
查看>>
vc2010, fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt解决办法
查看>>
linux sar 命令详解
查看>>
从30岁到35岁:为你的生命多积累一些厚度【转载】
查看>>
Android Java混淆(ProGuard)
查看>>
MySQL 可以用localhost 连接,但不能用IP连接的问题,局域网192.168.*.* 无法连接mysql...
查看>>
开发中遇到的编码的坑[开发篇]
查看>>
Git 基础 - 查看提交历史
查看>>
127.0.0.1
查看>>
《FPGA全程进阶---实战演练》第二章之硬件平台的搭建
查看>>
Oracle中如何实现Mysql的两表关联update操作
查看>>
Linux keepalived与lvs的深入分析
查看>>
Android开发日记(六)
查看>>
[Linux]Centos git报错fatal: HTTP request failed
查看>>
字符设备驱动[深入]:linux cdev详解
查看>>
python(4) 小程序-异步加载
查看>>
android电话接通状态下,关机铃声无法从外放输出
查看>>
button与input[type=”button”]的区别
查看>>
Android ImageView(scaleType属性)图片按比例缩放
查看>>