C#将大量数据批量写入Excel中

1、方法一:常规方法,适用于数据量较少,需要写入的列也少的情况,数据量大(列多)时速度非常慢

Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xApp.Visible = false;
Microsoft.Office.Interop.Excel.Workbook xBook = null;
Microsoft.Office.Interop.Excel.Worksheet xSheet = null;
Microsoft.Office.Interop.Excel.Range rng1 = null;

xBook = xApp.Workbooks.Open(xlsfile, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
xSheet = xBook.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

int rowindex = 4;
for (int c = 0; c < ZDtable.Rows.Count; c++){
    string szddm = ZDtable.Rows[c]["ZDDM"].ToString();
    string sqlr = ZDtable.Rows[c]["QLR"].ToString();
    //常规方法,适用于数据量较少,需要写入的列也少的情况,数据量大(列多)时速度非常慢
    rng1 = xSheet.get_Range("C" + (rowindex + c).ToString(), System.Type.Missing);//第1列
    rng1.Value2 = szddm;
    rng1 = xSheet.get_Range("E" + (rowindex + c).ToString(), System.Type.Missing);//第3列
    rng1.Value2 = sqlr;
}

xBook.Close(true);
Marshal.ReleaseComObject(xBook);
Marshal.ReleaseComObject(xSheet);
Marshal.ReleaseComObject(rng1);
xBook = null;
xSheet = null;
rng1 = null;
if (xApp != null)
{                                   
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xApp);
    xApp = null;
}
System.Diagnostics.Process[] excelprocess2 = System.Diagnostics.Process.GetProcessesByName("WINEXCEL");
foreach (System.Diagnostics.Process pr in excelprocess2)
{
   pr.Kill();//停止关联进程
}

2、方法二:二维数组法,适用于批量写入数据,数据量大(列多)时速度也非常快(推荐)

Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
xApp.Visible = false;
Microsoft.Office.Interop.Excel.Workbook xBook = null;
Microsoft.Office.Interop.Excel.Worksheet xSheet = null;
Microsoft.Office.Interop.Excel.Range range = null;

xBook = xApp.Workbooks.Open(xlsfile, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
xSheet = xBook.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet;

//创建适应需求的二维数组,第一个参数表示行数,第二个参数表示列数
object[,] cellData = new object[ZDtable.Rows.Count, 3];

for (int c = 0; c < ZDtable.Rows.Count; c++){
    string szddm = ZDtable.Rows[c]["ZDDM"].ToString();
    string sqlr = ZDtable.Rows[c]["QLR"].ToString();
    //二维数组法,适用于批量写入数据,数据量大(列多)时速度也非常快
    cellData[c, 0] = szddm;//第1列
    cellData[c, 2] = sqlr;//第3列,第二列不需要写入数据
}

//将二维数组存入sheet文件中
int rows = cellData.GetLength(1);
int colums = cellData.GetLength(0);
//写入数据行
range = xSheet.get_Range("C4", Missing.Value);//数据起始位置
range = range.get_Resize(colums, rows);//数据范围
range.set_Value(Missing.Value, cellData);//将数组插入sheet中

xBook.Close(true);
Marshal.ReleaseComObject(xBook);
Marshal.ReleaseComObject(xSheet);
Marshal.ReleaseComObject(range);
xBook = null;
xSheet = null;
range = null;
if (xApp != null)
{                                   
    System.Runtime.InteropServices.Marshal.ReleaseComObject(xApp);
    xApp = null;
}
System.Diagnostics.Process[] excelprocess2 = System.Diagnostics.Process.GetProcessesByName("WINEXCEL");
foreach (System.Diagnostics.Process pr in excelprocess2)
{
   pr.Kill();//停止关联进程
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值