首页 新闻 会员 周边

asp.net 数据导入到excel性能问题

0
悬赏园豆:10 [已解决问题] 解决于 2015-01-07 09:26

我现在又一个1500行左右,35列的grid,要把里面的数据导入到excel中,花了大概三四分钟,这样的性能肯定的不行的,有没有人有什么好办法,可以提升性能的方法?

没有同名的主页 没有同名 | 初学一级 | 园豆:28
提问于:2015-01-06 16:02
< >
分享
最佳答案
0

1、这样的性能为什么肯定不行呢?

2、如果说不行的话,你是怎么导的,代码呢?

3、你的一行三十五列里面平均大约有多少个字符?

4、最简单的方法,升级CPU、内存、硬盘。

收获园豆:10
爱编程的大叔 | 高人七级 |园豆:30839 | 2015-01-06 16:29

1.三四分钟的时间,正常的用户都等不下去

2.前人留下的代码是把grid中的内容一个一个单元格复制到excel中,我觉得这种方法太傻,应该会有更好的方法,所以过来问一下

3.字符少的一个,多的几十个,看用户填的时候填多少了,我无法估计

4.我觉得速度慢最主要的原因是嵌套了多层循环,和cpu、内存、硬盘有关?

没有同名 | 园豆:28 (初学一级) | 2015-01-06 16:35

@没有同名: 

1、正常用户也不会在WEB页面上打开1500行的表格看的。

2、ASP.NET,看你的意思,没有说清楚,只能是估计了,是用JAVASCRIPT遍历表格是吗?

Javascript的德性就那样,你觉得太傻,把代码贴一下问题也不大吧?

3、一行字符数的估计,就象你看到的微软面试题一样,估计北京有多少架钢琴,你既然不会回答,那也没有办法。既然要求是估计,就是怕有你的字段里面,有人写个5万字长文。

所谓的估计,就是要一个数量级,100字/300字/700字/1000字/10000字,不会当我没说。

4、所有软件运行速度的原因都跟CPU、内存、硬盘有关。

爱编程的大叔 | 园豆:30839 (高人七级) | 2015-01-06 16:46

@爱编程的大叔: 

1.因为这个系统和工资有关系,所以这个不正常的用户必须每个月都至少导出一遍,然后一条一条对过去

2.

foreach (GridViewRow row in gv.Rows)
{
for (int e = 3; e < row.Cells.Count; e++)
{
if (gvType == "Analysis")
{
switch (e)
{
#region gvAnalysis

case 3:
oRng = oSheet.Range[oSheet.Cells[j, 1], oSheet.Cells[j, 1]];
oRng.NumberFormat = "000000";
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[4].Text);
break;
case 4:
oRng = oSheet.Range[oSheet.Cells[j, 2], oSheet.Cells[j, 2]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[5].Text);
break;
case 5:
oRng = oSheet.Range[oSheet.Cells[j, 3], oSheet.Cells[j, 3]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[6].Text);
break;
case 6:
oRng = oSheet.Range[oSheet.Cells[j, 4], oSheet.Cells[j, 4]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[7].Text);
break;
case 7:
oRng = oSheet.Range[oSheet.Cells[j, 5], oSheet.Cells[j, 5]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[8].Text);
break;
case 8:
oRng = oSheet.Range[oSheet.Cells[j, 6], oSheet.Cells[j, 6]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[9].Text);
break;
case 9:
oRng = oSheet.Range[oSheet.Cells[j, 7], oSheet.Cells[j, 7]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[12].Text);
break;
case 10:
oRng = oSheet.Range[oSheet.Cells[j, 8], oSheet.Cells[j, 8]];
TextBox tbStandardHours = row.Cells[11].FindControl("tbHours") as TextBox;
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(tbStandardHours.Text);
break;
case 11:
oRng = oSheet.Range[oSheet.Cells[j, 9], oSheet.Cells[j, 9]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[14].Text);
break;
case 12:
oRng = oSheet.Range[oSheet.Cells[j, 10], oSheet.Cells[j, 10]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[15].Text);
break;
case 13:
oRng = oSheet.Range[oSheet.Cells[j, 11], oSheet.Cells[j, 11]];
TextBox tbExpectedHours = row.Cells[14].FindControl("tbExpectedHours") as TextBox;
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(tbExpectedHours.Text);
break;
case 14:
oRng = oSheet.Range[oSheet.Cells[j, 12], oSheet.Cells[j, 12]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[17].Text);
break;
case 15:
oRng = oSheet.Range[oSheet.Cells[j, 13], oSheet.Cells[j, 13]];
TextBox tbEffectiveHours = row.Cells[16].FindControl("tbEffectiveHours") as TextBox;
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(tbEffectiveHours.Text);
break;
case 16:
oRng = oSheet.Range[oSheet.Cells[j, 14], oSheet.Cells[j, 14]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[11].Text);
break;
case 17:
oRng = oSheet.Range[oSheet.Cells[j, 15], oSheet.Cells[j, 15]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[18].Text);
break;
case 18:
oRng = oSheet.Range[oSheet.Cells[j, 16], oSheet.Cells[j, 16]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[19].Text);
break;
case 19:
oRng = oSheet.Range[oSheet.Cells[j, 17], oSheet.Cells[j, 17]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[20].Text);
break;
case 20:
oRng = oSheet.Range[oSheet.Cells[j, 18], oSheet.Cells[j, 18]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[21].Text);
break;
case 21:
oRng = oSheet.Range[oSheet.Cells[j, 19], oSheet.Cells[j, 19]];
TextBox tbChargedHours = row.Cells[22].FindControl("tbChargedHours") as TextBox;
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(tbChargedHours.Text);
break;
case 22:
oRng = oSheet.Range[oSheet.Cells[j, 20], oSheet.Cells[j, 20]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[23].Text);
break;
case 23:
oRng = oSheet.Range[oSheet.Cells[j, 21], oSheet.Cells[j, 21]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[24].Text);
break;
case 24:
oRng = oSheet.Range[oSheet.Cells[j, 22], oSheet.Cells[j, 22]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[25].Text);
break;
case 25:
oRng = oSheet.Range[oSheet.Cells[j, 23], oSheet.Cells[j, 23]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[26].Text);
break;
case 26:
oRng = oSheet.Range[oSheet.Cells[j, 24], oSheet.Cells[j, 24]];
DropDownList ddlGapReason = row.Cells[27].FindControl("ddlGapReason") as DropDownList;
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(ddlGapReason.SelectedItem.Text);
break;
case 27:
oRng = oSheet.Range[oSheet.Cells[j, 25], oSheet.Cells[j, 25]];
TextBox remarkTB = row.Cells[28].FindControl("tbRemark") as TextBox;
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(remarkTB.ToolTip);
break;

default:
break;
#endregion
}
}
else
{
switch (e)
{
#region History

case 3:
oRng = oSheet.Range[oSheet.Cells[j, 1], oSheet.Cells[j, 1]];
oRng.NumberFormat = "000000";
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[3].Text);
break;
case 4:
oRng = oSheet.Range[oSheet.Cells[j, 2], oSheet.Cells[j, 2]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[4].Text);
break;
case 5:
oRng = oSheet.Range[oSheet.Cells[j, 3], oSheet.Cells[j, 3]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[5].Text);
break;
case 6:
oRng = oSheet.Range[oSheet.Cells[j, 4], oSheet.Cells[j, 4]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[6].Text);
break;
case 7:
oRng = oSheet.Range[oSheet.Cells[j, 5], oSheet.Cells[j, 5]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[7].Text);
break;
case 8:
oRng = oSheet.Range[oSheet.Cells[j, 6], oSheet.Cells[j, 6]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[8].Text);
break;
case 9:
oRng = oSheet.Range[oSheet.Cells[j, 7], oSheet.Cells[j, 7]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[11].Text);
break;
case 10:
oRng = oSheet.Range[oSheet.Cells[j, 8], oSheet.Cells[j, 8]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[12].Text);
break;
case 11:
oRng = oSheet.Range[oSheet.Cells[j, 9], oSheet.Cells[j, 9]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[13].Text);
break;
case 12:
oRng = oSheet.Range[oSheet.Cells[j, 10], oSheet.Cells[j, 10]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[14].Text);
break;
case 13:
oRng = oSheet.Range[oSheet.Cells[j, 11], oSheet.Cells[j, 11]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[15].Text);
break;
case 14:
oRng = oSheet.Range[oSheet.Cells[j, 12], oSheet.Cells[j, 12]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[16].Text);
break;
case 15:
oRng = oSheet.Range[oSheet.Cells[j, 13], oSheet.Cells[j, 13]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[9].Text);
break;
case 16:
oRng = oSheet.Range[oSheet.Cells[j, 14], oSheet.Cells[j, 14]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[10].Text);
break;
case 17:
oRng = oSheet.Range[oSheet.Cells[j, 15], oSheet.Cells[j, 15]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[17].Text);
break;
case 18:
oRng = oSheet.Range[oSheet.Cells[j, 16], oSheet.Cells[j, 16]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[18].Text);
break;
case 19:
oRng = oSheet.Range[oSheet.Cells[j, 17], oSheet.Cells[j, 17]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[19].Text);
break;
case 20:
oRng = oSheet.Range[oSheet.Cells[j, 18], oSheet.Cells[j, 18]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[20].Text);
break;
case 21:
oRng = oSheet.Range[oSheet.Cells[j, 19], oSheet.Cells[j, 19]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[21].Text);
break;
case 22:
oRng = oSheet.Range[oSheet.Cells[j, 20], oSheet.Cells[j, 20]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[22].Text);
break;
case 23:
oRng = oSheet.Range[oSheet.Cells[j, 21], oSheet.Cells[j, 21]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[23].Text);
break;
case 24:
oRng = oSheet.Range[oSheet.Cells[j, 22], oSheet.Cells[j, 22]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[24].Text);
break;
case 25:
oRng = oSheet.Range[oSheet.Cells[j, 23], oSheet.Cells[j, 23]];
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(row.Cells[25].Text);
break;
case 26:
oRng = oSheet.Range[oSheet.Cells[j, 24], oSheet.Cells[j, 24]];
DropDownList ddlGapReason = row.Cells[26].FindControl("ddlGapReason") as DropDownList;
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(ddlGapReason.SelectedItem.Text);
break;
case 27:
oRng = oSheet.Range[oSheet.Cells[j, 25], oSheet.Cells[j, 25]];
TextBox remarkTB = row.Cells[27].FindControl("tbRemark") as TextBox;
oRng.FormulaR1C1 = HttpUtility.HtmlDecode(remarkTB.ToolTip);
break;

default:
break;
#endregion
}
}
oRng.WrapText = false;
oRng.Borders.Color = ColorTranslator.ToOle(Color.Black);
oRng.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
}
j++;
}

3.估计十个字符左右吧

4.请忽略这个可以吗?讨论这个没有意义,难道我跑到客户面前告诉他,那么慢和我们的代码没有关系,都是你的电脑问题,请你换一个最新的最好的电脑再来跑我们的程序?

没有同名 | 园豆:28 (初学一级) | 2015-01-06 16:59

@没有同名: 

考虑问题是这样的:

1、如果每个月5日过后,上个月的数据就不会变化了。完全可以没事干的时候生成一个EXCEL放在那儿,想要的时候,点一个就可以下载了,根本不需要临时生成。

2、即使需要临时生成,也不需要再到GRIDVIEW里面去取,本身这些数据就是从数据库里面取出来,放在Datatable或者啥容器对象里面,直接将DataTableToExcel就行了。

3、第1条是很多程序员容易忽略的方法,累死了优化性能,不如转换下思路。

爱编程的大叔 | 园豆:30839 (高人七级) | 2015-01-06 17:14

@爱编程的大叔: 

1.的确是这个月导出上个月的数据,但是和有没有空没有关系,现在说的是性能问题,总不能让用户发呆三四分钟吧,正常的用户会怀疑是不是程序出错了,为什么没反应

2.我现在正在做的就是datatabletoexcel,只是不知道有没有更好地办法,前边的代码是前人留下的,我都说了很傻的代码了

3.亲,这个系统真不是我用的,如果是我用的,我才不在乎性能问题呢,才不会傻到一直该性能

没有同名 | 园豆:28 (初学一级) | 2015-01-06 17:20

@没有同名: 你有没有看懂我说的第一个方法?无语了。

爱编程的大叔 | 园豆:30839 (高人七级) | 2015-01-06 17:24

@爱编程的大叔: 

现在看了一下看明白了,刚刚失误一下,可惜的是这个不行,因为用户可以按条件筛选导出的内容,所以这个不适用

没有同名 | 园豆:28 (初学一级) | 2015-01-06 17:28

@没有同名: 

优化的方向非常多

1、还是我说的静态方法,用户需求有时候是不准确的。

2、Launcher的方法,最节省资源。

3、看你上面的导出代码,居然还去设置EXCEL的显示格式,真是无语了,这个完全没有必要的,真心这么体贴用户的话,完全可以做一个EXCEL模板,记住,导出只导数据,别理格式,格式完全可以事先设置好。

4、所有方法都用过后,速度还是慢是吧(升级服务器的CPU、内存,不是客户端的)

5、硬件方法不行了,再用程序员终极加速大法

“数据生成中,请稍候....”,下面开个小窗口播放《月光宝盒》。

然后用户会提意见,你们这个导出速度太快了,能不能弄慢一点,等我看完。

爱编程的大叔 | 园豆:30839 (高人七级) | 2015-01-06 17:44

@爱编程的大叔: 

哈哈,好吧啊,就你了!月光宝盒脑补了一下,笑死我了!

没有同名 | 园豆:28 (初学一级) | 2015-01-07 09:25
其他回答(3)
0

用datable 保存数据 ,再用npoi 导出啊. 

_jie | 园豆:116 (初学一级) | 2015-01-06 16:36

我试试

支持(0) 反对(0) 没有同名 | 园豆:28 (初学一级) | 2015-01-06 16:38
0

直接写 .csv 格式的文件。

Launcher | 园豆:45045 (高人七级) | 2015-01-06 16:41
0

怕是你插入数据库的问题,我到过比你大两倍的数据,没有优化的时候,需要5分钟(出入数据库时用的是持续连接),优化后只需要2秒(把所有插入写成一个字符串,只执行了一次数据操作)。

刘宏玺 | 园豆:14020 (专家六级) | 2015-01-06 16:55

和数据库没有关系,我是把表格里的数据导出到excel中

支持(0) 反对(0) 没有同名 | 园豆:28 (初学一级) | 2015-01-06 17:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册