我有一个C#项目,我需要更改Excel单元格中的值。这适用于某些部分而不是其他部分,这真的很奇怪。不起作用的部分在这里:
exApp = new Microsoft.Office.Interop.Excel.Application();
Excel.Workbook exWb = exApp.Workbooks.Open(filepath);
Excel.Worksheet SumSheet = exWb.Worksheets[1];
Excel.Range line = (Excel.Range)SumSheet.Rows[4];
line.Insert();
SumSheet.Range[SumSheet.Cells[13, 4], SumSheet.Cells[13, 4]].Value = "t1";
Debug.Print((string)SumSheet.Range[SumSheet.Cells[13, 4], SumSheet.Cells[13, 4]].Value);
SumSheet.Range[SumSheet.Cells[15, 4], SumSheet.Cells[15, 4]].Value = "t2";
SumSheet.Range[SumSheet.Cells[15, 4], SumSheet.Cells[15, 5]].FormulaR1C1 = "=SUMME(O6:O400)";
当我尝试更改值时,它会给我错误。 以下代码有效:
for (int i = 2; i <= exWb.Worksheets.Count; i++)
{
int counter = 0;
int counterMax = 0;
Excel.Worksheet exWs = exWb.Worksheets[i];
line = (Excel.Range)exWs.Rows[15];
line.NumberFormat = "##";
line.Insert();
line.Insert();
line.Insert();
for (int j = 2 ; j <= exWs.UsedRange.Columns.Count; j++)
{
if (exWs.Range[exWs.Cells[13, j], exWs.Cells[13, j]].Value <= 0.3)
{
counter ++;
exWs.Range[exWs.Cells[14, j], exWs.Cells[14, j]].Value = 1;
exWs.Range[exWs.Cells[14, j], exWs.Cells[14, j]].Interior.Color = Color.Green;
exWs.Range[exWs.Cells[15, j], exWs.Cells[15, j]].Value = counter;
exWs.Range[exWs.Cells[15, j], exWs.Cells[15, j]].Interior.Color = Color.Green;
counterMax = (counter > counterMax) ? counter : counterMax;
}
else
{
counter = 0;
}
}
exWs.Range[exWs.Cells[16, 2], exWs.Cells[16, 2]].Value = counterMax;
fillStart(exWs.Name, counterMax, SumSheet);
if (counterMax > 45)
{
exWs.Range[exWs.Cells[16, 2], exWs.Cells[16, 2]].Interior.Color = Color.Green;
}
else
{
exWs.Range[exWs.Cells[16, 2], exWs.Cells[16, 2]].Interior.Color = Color.Red;
}
}
有什么想法吗?