惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recorded Future
Recorded Future
Microsoft Security Blog
Microsoft Security Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
The Cloudflare Blog
B
Blog RSS Feed
小众软件
小众软件
博客园 - 叶小钗
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 聂微东
博客园_首页
B
Blog
雷峰网
雷峰网
S
SegmentFault 最新的问题
N
Netflix TechBlog - Medium
D
Docker
博客园 - 司徒正美
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
U
Unit 42
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
腾讯CDC
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
Jina AI
Jina AI
WordPress大学
WordPress大学
D
DataBreaches.Net
V
V2EX
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
F
Full Disclosure
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
The Hacker News
The Hacker News
Security Archives - TechRepublic
Security Archives - TechRepublic
IT之家
IT之家
P
Privacy International News Feed

博客园 - Leonny

IIS站点/虚拟目录中访问共享目录(UNC)以及建立后的应用程序的信任级别问题 谁说用Azure就一定要Vista/Win7?Window2003/xp下安装 Windows Azure Tools for Microsoft Visual Studio IE的window.showModalDialog出现内存不足的问题 - Leonny - 博客园 关于清理JBoss服务器端jsp文件缓存的问题 提取assemblyinfo.cs文件里的guid值 经常坐电脑旁的人必喝的健康饮品 Firefox图片模糊的问题 MS SQL Server2005如何得到记录的行号 [译]NUnitForms官方说明文档 C#取得MS Word的总页数 VS2008中文版安装ASP.NET MVC Beta WinForm下ComboBox添加项与设定预选项 成功申请了GAE Vista删你文件没商量 3大数据库的变量参数符号 新立得软件包管理器出错 Repeater中點擊按鈕事件時要注意頁面PostBack的問題 關於Web Service 中使用NHibernate時無法序列化IList的問題 如何取得輸入字符串的寬度
[原创]OWC生成Excel的效能优化
Leonny · 2008-11-25 · via 博客园 - Leonny

优化前的代码, 从DataTable导入1000笔数据要差不多10分钟,有时候还会超时或出错:

public void WriteDataToSpreadsheet2(SpreadsheetClass p_spreadsheet, DataTable p_dt, 
    
int p_iRow, int p_iCol, bool p_bWithCaption, int p_iStaredRecord, 
    
int p_iLimited, int[] p_iTextColumns)
{
    Worksheet f_sheet 
= p_spreadsheet.ActiveSheet;
    
int f_iRowCount = 0;
    
int f_iTotalRow = 0;
    
int f_iMaxRow = 65535;

    
int f_iRow, f_iCol, f_iDtColumn;

    f_iRow 
= p_iRow;
    f_iCol 
= p_iCol;
    
//使用标题
    if (p_bWithCaption == true)
    
{
        
foreach (DataColumn f_col in p_dt.Columns)
        
{
            f_sheet.Cells[f_iRow, f_iCol] 
= f_col.Caption;
            f_iCol
++;
        }

        f_iRow
++;
        f_iTotalRow
++;

        f_iCol 
= p_iCol;
    }


    
foreach (DataRow f_row in p_dt.Rows)
    
{
        f_iDtColumn 
= 0;
        
foreach (DataColumn f_col in p_dt.Columns)
        
{
            
if (f_row[f_col] != DBNull.Value)
            
{
                
//注意,这里是一列一列的赋值
                if (Array.IndexOf(p_iTextColumns, f_iDtColumn) > -1)
                    f_sheet.Cells[f_iRow, f_iCol] 
= "'" + f_row[f_col].ToString();    //强制转换成字符串
                else
                    f_sheet.Cells[f_iRow, f_iCol] 
= f_row[f_col];
            }

            f_iDtColumn
++;
            f_iCol
++;
        }


        f_iRowCount
++;
        f_iTotalRow
++;

        
if (f_iTotalRow >= f_iMaxRow)
            
break;

        
if (p_iLimited > 0)
        
{
            
if (f_iRowCount >= p_iLimited)
                
break;
        }

        f_iRow
++;
        f_iCol 
= p_iCol;
    }

    
return;
}

 优化后的代码, 从DataTable导入60000(注意,是六万)笔数据, 只要不到两分钟.

 1public void WriteDataToSpreadsheet(SpreadsheetClass p_spreadsheet, DataTable p_dt,
 2    int p_iRow, int p_iCol, bool p_bWithCaption, int p_iStaredRecord, 
 3    int p_iLimited, int[] p_iTextColumns)
 4{
 5    Worksheet f_sheet = p_spreadsheet.ActiveSheet;
 6    int f_iRowCount = 0;
 7    int f_iTotalRow = 0;
 8    int f_iMaxRow = 65535;
 9
10    int f_iRow, f_iCol, f_iDtColumn;
11
12    f_iRow = p_iRow;
13    f_iCol = p_iCol;
14
15    if (p_bWithCaption == true)
16    {
17        foreach (DataColumn f_col in p_dt.Columns)
18        {
19            f_sheet.Cells[f_iRow, f_iCol] = f_col.Caption;
20            f_iCol++;
21        }

22        f_iRow++;
23        f_iTotalRow++;
24
25        f_iCol = p_iCol;
26    }

27
28    string[] A2Z = new string[26"A""B""C""D""E""F""G"
29        "H""I""J""K""L""M""N""O""P""Q""R",
30        "S""T""U""V""W""X""Y""Z" }
;
31    List<string> A2ZZ = new List<string>();
32    A2ZZ.AddRange(A2Z);
33    foreach (string en in A2Z)
34    {
35        foreach (string en2 in A2Z)
36        {
37            A2ZZ.Add(en + en2);
38        }

39    }

40    string lastColumnName = A2ZZ[p_dt.Columns.Count - 1];
41
42    List<object[,]> list = new List<object[,]>();
43    Range r = f_sheet.get_Range("A" + f_iRow.ToString(), lastColumnName + f_iRow.ToString());
44    int row = p_dt.Rows.Count;
45    int col = p_dt.Columns.Count;
46
47    if (row > 0)
48    {
49        //先将每一行资料放入一个数组,然后再放入清单
50        for (int i = 0; i < row; i++)
51        {
52            object[,] objData = new Object[1, col];
53            for (int j = 0; j < col; j++)
54            {
55                if (p_dt.Rows[i][j] != DBNull.Value)
56                {
57                    if (Array.IndexOf(p_iTextColumns, j) > -1)
58                        objData[0, j] = "'" + p_dt.Rows[i][j].ToString();
59                    else
60                        objData[0, j] = p_dt.Rows[i][j];
61                }

62                else objData[0, j] = "";
63            }

64            list.Add(objData);
65        }

66    }

67    object m_objOpt = System.Reflection.Missing.Value;
68    for (int i = 0; i < row; i++)
69    {
70        //直接到一个区间,然后对区间直接赋数组作为值, 这是真正提速的地方< span style="color: #008000;">
71        Range r2 = r.get_Range("A" + (f_iRowCount+1).ToString(), lastColumnName + (f_iRowCount+1).ToString());
72        r2.set_Value(m_objOpt, list[i]);
73
74        f_iRowCount++;
75        f_iTotalRow++;
76
77        if (f_iTotalRow >= f_iMaxRow)
78            break;
79
80        if (p_iLimited > 0)
81        {
82            if (f_iRowCount >= p_iLimited)
83                break;
84        }

85        f_iRow++;
86        f_iCol = p_iCol;
87    }

88    return;
89}
90

上面一些代码是有其他用的, 大家只要看注释那块的就行了. 人太懒, 不好意思. 呵呵.

这个方法只是我在项目中使用的代码, 时间比较紧, 肯定还有更快的方法, 如果各位有更好的方法, 请赐教.