我使用:
worksheet.Cells[row,column] = string;
这样的方式给某个Cell赋值。如果之前Cell有值的话,那么这样是正常的。
但是如果一旦Cell里面的值本身是空或者空格,这个就会导致Excel表里面会出现莫名其妙的故障,比如此Cell和其他Cell突然变成了其他奇怪的内容。求助..
worksheet.Cells[row,column] = string;
这样的方式给某个Cell赋值。如果之前Cell有值的话,那么这样是正常的。
但是如果一旦Cell里面的值本身是空或者空格,这个就会导致Excel表里面会出现莫名其妙的故障,比如此Cell和其他Cell突然变成了其他奇怪的内容。求助..
而是当Cell处于编辑状态时,对其操作就会出错。求解决方法.....
{
rowIndex ++;
colIndex = 0;
foreach(string columnname in columns)
{
DataColumn col=dv.Table.Columns[columnname]; colIndex++; if(col.DataType == System.Type.GetType("System.DateTime"))
{
excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
}
else if(col.DataType == System.Type.GetType("System.String"))
{
excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
}
else
{
excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
} int sumindex=colIndex-2;
switch(col.DataType.FullName)
{
case "System.Int32":
case "System.Single":
case "System.Double":
case "System.Decimal":
object o=row.Row[col];
if(!Convert.IsDBNull(o))
sums[sumindex]+=Convert.ToDecimal(o);
break;
}
}
}
不,你的这段代码仍然没有处理异常情况.
你可以尝试在这段代码加一段异常捕获,然后你就会发现当你在Cell的编辑模式下 执行这段代码的时候会抛出异常..
不需要判断什么是否在编辑模式。
统一的在数据写入的时候都Workbook.Save一下就OK了.
酱紫全部都变成不是在编辑状态了.