双层for求优化

将你刻在我心底 2016-01-15 05:58:41
private void Classification(DataTable dt, string[] allType, string[] citylist, ref int startIndex, ref int sheet3Index, ref int sheet6Index, Excel.Worksheet worksheet2, Excel.Worksheet worksheet3, Excel.Worksheet worksheet6, ref string filter)
{
#region 业务类型1、2(第二、三、六个sheet)
for (int m = 0; m < allType.Length; m++)//共48条
{
if (条件)
{
filter = "条件";
Category(dt, allType[m], citylist, ref startIndex, ref sheet3Index, ref sheet6Index, worksheet2, worksheet3, worksheet6, filter);
}
else if (条件)
{
filter = "条件";
Category(dt, allType[m], citylist, ref startIndex, ref sheet3Index, ref sheet6Index, worksheet2, worksheet3, worksheet6, filter);
}
else if (条件)
{
filter = "条件";
Category(dt, allType[m], citylist, ref startIndex, ref sheet3Index, ref sheet6Index, worksheet2, worksheet3, worksheet6, filter);
}
else if (条件)
{
filter = "条件";
Category(dt, allType[m], citylist, ref startIndex, ref sheet3Index, ref sheet6Index, worksheet2, worksheet3, worksheet6, filter);
}
else
{
filter = "条件";
Category(dt, allType[m], citylist, ref startIndex, ref sheet3Index, ref sheet6Index, worksheet2, worksheet3, worksheet6, filter);
}
sheet3Index++;
sheet6Index++;
}
#endregion
}


/// <param name="dt">源表</param>
/// <param name="Type"></param>
/// <param name="citylist">城市集合</param>
/// <param name="startIndex">第二个sheet起始行</param>
/// <param name="sheet3Index">第三个sheet起始行</param>
/// <param name="sheet6Index">第二六sheet起始行</param>
/// <param name="worksheet2">第二个sheet</param>
/// <param name="worksheet3">第三个sheet</param>
/// <param name="worksheet6">第六个sheet</param>
/// <param name="filter">过滤条件</param>
private void Category(DataTable dt, string Type, string[] citylist, ref int startIndex, ref int sheet3Index, ref int sheet6Index, Excel.Worksheet worksheet2, Excel.Worksheet worksheet3, Excel.Worksheet worksheet6,string filter)
{
#region 第二个sheet
int count;
count = (int)dt.Compute("Count(列名)", filter + "条件");
worksheet2.Cells[startIndex + 1, 4] = count;
startIndex++;
#endregion
for (int i = 0; i < citylist.Length; i++)//共16条
{
string curCity = citylist[i].ToString();
#region 第三sheet
count = (int)dt.Compute("Count(列名)", "故障地市='" + curCity + "' and " + filter);

//count = (int)dt.Compute("Count(故障地市)", "故障地市='" + curCity + "' and 业务类型 like '%" + allType[m] + "'");
if (sheet3Index == 21 || sheet3Index == 37 || sheet3Index == 45 || sheet3Index == 52 || sheet3Index == 58)
{ sheet3Index += 2; }
worksheet3.Cells[sheet3Index, 5 + i] = count;
#endregion

#region 第六个sheet
if (sheet6Index == 22 || sheet6Index == 38 || sheet6Index == 46 || sheet6Index == 53 || sheet6Index == 59)
{ sheet6Index += 2; }
count = (int)dt.Compute("Count(列名)", "列名='" + curCity + "'and " + filter + "条件");
worksheet6.Cells[6 + i * 8, sheet6Index] = count;
count = (int)dt.Compute("Count(列名)", "列名='" + curCity + "'and " + filter + "条件");
worksheet6.Cells[7 + i * 8, sheet6Index] = count;
count = (int)dt.Compute("Count(列名)", "列名='" + curCity + "'and " + filter + "条件");
worksheet6.Cells[8 + i * 8, sheet6Index] = count;
count = (int)dt.Compute("Count(列名)", "列名='" + curCity + "'and " + filter + "条件");
worksheet6.Cells[9 + i * 8, sheet6Index] = count;
count = (int)dt.Compute("Count(列名)", "列名='" + curCity + "'and " + filter + "条件'");
worksheet6.Cells[10 + i * 8, sheet6Index] = count;
count = (int)dt.Compute("Count(列名)", "列名='" + curCity + "'and " + filter + "条件");
worksheet6.Cells[11 + i * 8, sheet6Index] = count;
count = (int)dt.Compute("Count(列名)", "列名='" + curCity + "'and " + filter + "条件");
worksheet6.Cells[12 + i * 8, sheet6Index] = count;
count = (int)dt.Compute("Count(列名)", "列名='" + curCity + "'and " + filter + "条件");
worksheet6.Cells[13 + i * 8, sheet6Index] = count;
#endregion
}
}
48*16 == 768,内部还有Computer,速度非常慢,求大神支招。主要是与Excel交互。不给用数据库
...全文
211 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
无法结贴,什么鬼
  • 打赏
  • 举报
回复
http://www.cnblogs.com/teacherz/articles/2353819.html。法三最快。
  • 打赏
  • 举报
回复
引用 2 楼 From_TaiWan 的回复:
按单元格逐个写入,肯定慢,可以把excel看作数据库,sheet看作数据库的表,用Oledb连接,按照数据库操作方式来
我是用oledb连接的,怎么一次性写入,不会 。是先用循环获取值,在循环一个个写吗?
  • 打赏
  • 举报
回复
2#,我是用oledb连接的,怎么一次性写入,不会
  • 打赏
  • 举报
回复
引用 5 楼 From_TaiWan 的回复:
看你代码,是用Excel类操作的,不是oledbconnection,给你链接http://blog.sina.com.cn/s/blog_6d86f75501019s92.html 参考下,往sheet里插入数据,insert into [$Sheet3](C1,C2……,应该快很多
如何插入至excel指定列,因为模板别人做好的
秋的红果实 2016-01-16
  • 打赏
  • 举报
回复
看你代码,是用Excel类操作的,不是oledbconnection,给你链接http://blog.sina.com.cn/s/blog_6d86f75501019s92.html 参考下,往sheet里插入数据,insert into [$Sheet3](C1,C2……,应该快很多
秋的红果实 2016-01-15
  • 打赏
  • 举报
回复
按单元格逐个写入,肯定慢,可以把excel看作数据库,sheet看作数据库的表,用Oledb连接,按照数据库操作方式来
快乐起航2020 2016-01-15
  • 打赏
  • 举报
回复
一个小的优化方法是先获取Length , int len=allType.Length; for (int m = 0; m < len; m++)//共48条 { 、、、 再进入循环,我们在外部获取它的长度,这样不用每次都要统计它的长度,小技巧嘿

111,128

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧