高手请进!!!程序运行太慢的问题.....

nickzwd 2006-03-22 05:06:55
哪位大哥能帮忙把程序给优化一下阿????现在这种循环速度慢的要死了......





private void button1_Click(object sender, System.EventArgs e)
{
Excel.ApplicationClass excel = new Excel.ApplicationClass();
_Workbook workbook=null;
try
{
workbook=excel.Workbooks._Open(txtExcelFile.Text,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
_Worksheet worksheet=(_Worksheet)workbook.Worksheets.get_Item(1);
string curCell = Combine2Int(1,1);
string curCell2= Combine2Int(worksheet.UsedRange.Rows.Count,worksheet.UsedRange.Columns.Count);
Excel.Range rg=worksheet.get_Range(curCell,curCell2);
OWC10.Range rg2 = axSpreadsheet1.ActiveSheet.get_Range(curCell,curCell2);
rg2.Value2=rg.Value2;
/**样式*/

for(int i=1;i<=worksheet.UsedRange.Rows.Count;i++)
{
for(int j=1;j<=worksheet.UsedRange.Columns.Count;j++)
{
curCell=Combine2Int(i,j);
rg=worksheet.get_Range(curCell,curCell);
rg2 = axSpreadsheet1.ActiveSheet.get_Range(curCell,curCell);
rg2.set_ColumnWidth((double)rg.ColumnWidth);
rg2.set_RowHeight((double)rg.RowHeight);
rg2.Font.set_Underline((OWC10.XlUnderlineStyle)rg.Font.Underline);
rg2.Font.set_Bold((bool)rg.Font.Bold);
rg2.Font.set_Italic((bool)rg.Font.Italic);
rg2.Font.set_Name((string)rg.Font.Name);
rg2.Font.set_Size((double)rg.Font.Size);
object fontColor =rg.Font.Color;
rg2.Font.set_Color(ref fontColor);
object color = rg.Interior.Color;
rg2.Interior.set_Color(ref color);
rg2.Borders[OWC10.XlBordersIndex.xlEdgeBottom].set_LineStyle((OWC10.XlLineStyle)rg.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle);
color=rg.Borders[Excel.XlBordersIndex.xlEdgeBottom].Color;
rg2.Borders[OWC10.XlBordersIndex.xlEdgeBottom].set_Color(ref color);
rg2.Borders[OWC10.XlBordersIndex.xlEdgeBottom].set_Weight((OWC10.XlBorderWeight)rg.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight);

rg2.Borders[OWC10.XlBordersIndex.xlEdgeLeft].set_LineStyle((OWC10.XlLineStyle)rg.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle);
color=rg.Borders[Excel.XlBordersIndex.xlEdgeLeft].Color;
rg2.Borders[OWC10.XlBordersIndex.xlEdgeLeft].set_Color(ref color);
rg2.Borders[OWC10.XlBordersIndex.xlEdgeLeft].set_Weight((OWC10.XlBorderWeight)rg.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight);

rg2.Borders[OWC10.XlBordersIndex.xlEdgeRight].set_LineStyle((OWC10.XlLineStyle)rg.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle);
color=rg.Borders[Excel.XlBordersIndex.xlEdgeRight].Color;
rg2.Borders[OWC10.XlBordersIndex.xlEdgeRight].set_Color(ref color);
rg2.Borders[OWC10.XlBordersIndex.xlEdgeRight].set_Weight((OWC10.XlBorderWeight)rg.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight);

rg2.Borders[OWC10.XlBordersIndex.xlEdgeTop].set_LineStyle((OWC10.XlLineStyle)rg.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle);
color=rg.Borders[Excel.XlBordersIndex.xlEdgeTop].Color;
rg2.Borders[OWC10.XlBordersIndex.xlEdgeTop].set_Color(ref color);
rg2.Borders[OWC10.XlBordersIndex.xlEdgeTop].set_Weight((OWC10.XlBorderWeight)rg.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight);

rg2.set_VerticalAlignment((OWC10.XlVAlign)rg.VerticalAlignment);
rg2.set_HorizontalAlignment((OWC10.XlHAlign)rg.HorizontalAlignment);
rg2.set_NumberFormat((string)rg.NumberFormat);

}
}

}
catch(Exception ex)
{
MessageBox.Show(ex.Source);
}
finally
{
workbook.Close(false,Type.Missing,Type.Missing);
excel.Quit();
KillProcess();
}
}
private void KillProcess()
{
System.Diagnostics.Process[] myProcess;
myProcess=System.Diagnostics.Process.GetProcessesByName("EXCEL");
foreach(System.Diagnostics.Process process in myProcess)
{
process.Kill();
}
}
public string Combine2Int(int row ,int col)
{
char [] colArr = {' ','a','b','c','d','e','f','g','h','i','j','k','l'
,'m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

int a ,b ;
string sTemp ;

if ( col < 27)
sTemp = colArr[col].ToString();
else
{
a = (col - 1) /26;
b = col - a * 26;
sTemp = colArr[a].ToString() + (string)colArr[b].ToString();
}

return sTemp + row.ToString();
}
...全文
111 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nickzwd 2006-03-22
  • 打赏
  • 举报
回复
再看看...
allnew2006 2006-03-22
  • 打赏
  • 举报
回复
自己写的吗?可以用断点来测试程序循环路径,哪些不必要的去掉。。
livode 2006-03-22
  • 打赏
  • 举报
回复
表格的格式设置可以放到循环外面,没有必要一格一格的设。
lovvver 2006-03-22
  • 打赏
  • 举报
回复
1.算法优化;
2.即使释放资源,清空垃圾。
iseelxj 2006-03-22
  • 打赏
  • 举报
回复
... 太多了

110,526

社区成员

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

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

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