c# 数据放入excel导出,卡顿

前言:导出数据到Excel,导出的时候特别卡顿

原代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IPC.Helper
{
    class ExcelExportUtil
    {
 
        public static string expotExcel(List<DataTable> hisChartTables,string[] typeNames,string saveFileName)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                if (xlApp == null)
                {
                    return "无法创建Excel对象,您的电脑可能未安装Excel";
                }
 
                Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet3 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
                Microsoft.Office.Interop.Excel.Worksheet worksheet2 = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets.Add(Type.Missing,Type.Missing,Type.Missing,Type.Missing);
                worksheet.Name = typeNames[0];
 
                //写入标题
                int titleCount = 1;
                worksheet.Cells[1, titleCount++] = "时间1";
                worksheet.Cells[1, titleCount++] = "参数值";
                worksheet.Cells[1, titleCount++] = "最大值";
                worksheet.Cells[1, titleCount++] = "最小值";
 
 
                // 获取当前时间 
                DateTime now = DateTime.Now;
                string formattedTime = now.ToString("yyyy-MM-dd HH:mm:ss");
                Console.WriteLine(formattedTime);
                 
                //写入数值
                for (int r = 0; r < hisChartTables[0].Rows.Count; r++)
                {
                    int rowCount = 1;
                    worksheet.Cells[r + 2, rowCount++] = " " + hisChartTables[0].Rows[r]["data_time"].ToString() + "\t";
                    worksheet.Cells[r + 2, rowCount++] = hisChartTables[0].Rows[r]["conc"].ToString();
                    worksheet.Cells[r + 2, rowCount++] = hisChartTables[0].Rows[r]["max"].ToString();
                    worksheet.Cells[r + 2, rowCount++] = hisChartTables[0].Rows[r]["min"].ToString();
                    System.Windows.Forms.Application.DoEvents();
                }
                 
                // 获取当前时间 
                DateTime now1 = DateTime.Now;
                string formattedTime1 = now1.ToString("yyyy-MM-dd HH:mm:ss");
                Console.WriteLine(formattedTime1);
                 
                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
 
                workbook.Saved = true;
                workbook.SaveCopyAs(saveFileName);  //fileSaved = true;
 
                xlApp.Quit();
                GC.Collect();//强行销毁
 
                return "导出成功";
            }
            catch(Exception e)
            {
                return e.Message;
            }
 
              
        }
 
    }
}

  代码中增加了时间记录,发现循环写入这里,近3000条数据要耗时35s左右。于是进行优化,最终优化到仅1s。

优化思路:1、for循环中,遍历时count单独写在外面,否则每次.size()会耗时;

                  2、写入Excel时,不要单个单元格写入,使用range范围写入;

写入数据部分,修改后代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
object[,] datasHead = worksheet.Range["A1", "D1"].Value2;
datasHead[1, 1] = "时间";
datasHead[1, 2] = "参数值";
datasHead[1, 3] = "最大值";
datasHead[1, 4] = "最小值";
worksheet.Range["A1", "D1"].Value2 = datasHead;
 
//写入数值
int rowCounts = hisChartTables[0].Rows.Count;
string lastRange = "D" + rowCounts + 1;
object[,] datas = worksheet.Range["A2", lastRange].Value2;     
 
for (int r = 0; r < rowCounts; r++)
{
    datas[r+1, 1] = " " + hisChartTables[0].Rows[r]["data_time"].ToString() + "\t";
    datas[r+1, 2] = hisChartTables[0].Rows[r]["conc"].ToString();
    datas[r + 1, 3] = hisChartTables[0].Rows[r]["max"].ToString();
    datas[r + 1, 4] = hisChartTables[0].Rows[r]["min"].ToString();
 
    System.Windows.Forms.Application.DoEvents();
}
worksheet.Range["A2", lastRange].Value2 = datas;

  

 

posted on   七七2020  阅读(152)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示