怎么用Microsoft.Office.Interop.Excel来实现隔行染色呢?

CypressXT 2015-11-11 03:49:05


private void excelParsing()
{
Microsoft.Office.Interop.Excel.Workbooks ExcelDoc = ExcelApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook MyWorkBook = ExcelDoc.Add(System.Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet xlSheet = (Microsoft.Office.Interop.Excel.Worksheet)MyWorkBook.Worksheets[1];
ExcelApp.DisplayAlerts = false;
ExcelApp.Visible = false;

int i,j;
for (i = 0; i < 1280; i++)
{
xlSheet.Cells[i + 1, 1] = EventRecordArr[i, 0];
xlSheet.Cells[i + 1, 2] = EventRecordArr[i, 1];
xlSheet.Cells[i + 1, 3] = EventRecordArr[i, 2];
xlSheet.Cells[i + 1, 4] = EventRecordArr[i, 3];
if (i % 2 == 1)
{
for (j = 1; i < xlSheet.Columns.Count; j++)
((Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[i, j]).Interior.ColorIndex = 15;
}
}
Microsoft.Office.Interop.Excel.Range excelRange = xlSheet.get_Range(xlSheet.Cells[1, 1], xlSheet.Cells[1280, 4]);
excelRange.EntireColumn.AutoFit();
xlSheet.SaveAs(xlsxfileName);
ExcelDoc.Close();
ExcelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp);
ExcelApp = null;
}



运行时程序卡住,一暂停就停在
((Microsoft.Office.Interop.Excel.Range)xlSheet.Cells[i, j]).Interior.ColorIndex = 15;
这一行
...全文
168 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
CypressXT 2015-11-11
  • 打赏
  • 举报
回复
引用 2 楼 xdashewan 的回复:
[quote=引用 1 楼 xdashewan 的回复:]

for (j = 1; i < xlSheet.Columns.Count; j++)
看出区别了吗
for (j = 1; i < xlSheet.Columns.Count; j++)[/quote] 无语。。。一定是 昨天夜里手剁坏了
CypressXT 2015-11-11
  • 打赏
  • 举报
回复
无语。。。一定是 昨天夜里手剁坏了
xdashewan 2015-11-11
  • 打赏
  • 举报
回复
引用 1 楼 xdashewan 的回复:

for (j = 1; i < xlSheet.Columns.Count; j++)
看出区别了吗
for (j = 1; i < xlSheet.Columns.Count; j++)
xdashewan 2015-11-11
  • 打赏
  • 举报
回复

for (j = 1; i < xlSheet.Columns.Count; j++)
看出区别了吗

110,570

社区成员

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

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

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