我正在使用Visual Studio 2008,MS OFFICE 2010.(在WIN 7)
当我通过我的程序制作EXCEL文件时,用c#制作,无法应用单元格格式更改。 但这个问题仅在STRING ARRAY制作时引起。 如果仅输入一个单元格,则运行良好。(例如_SRange [&#34; A0&#34;,&#34; A0&#34;]。Value2 =&#34; 2018-02-13&#34 ;;)< / p>
有人帮帮我吗? 代码是这样的。
public void ExportOrderExcel(System.Data.DataTable dt, string Name, List<string> colNames)
{
System.Data.DataTable manufacturedDt = this.ManufatureDtForExcel(dt, colNames);
Application app = new ApplicationClass();
if (app == null)
{
System.Windows.Forms.MessageBox.Show("not started EXCEL");
return;
}
Workbooks workbooks = app.Workbooks;
_Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Sheets sheets = workbook.Worksheets;
_Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
if (worksheet == null)
{
System.Windows.Forms.MessageBox.Show("not exist WORKSHEET");
}
int titleRowCnt = 1;
int columnRowCnt = 1;
int totalRowCnt= manufacturedDt.Rows.Count + titleRowCnt + columnRowCnt;
object startPoint = worksheet.Cells[1, 1];
object endPoint = worksheet.Cells[(totalRowCnt), manufacturedDt.Columns.Count];
_SRange = worksheet.get_Range(startPoint, endPoint);
string[,] array1 = new string[totalRowCnt, manufacturedDt.Columns.Count];
//set title
_SRange.get_Range(GetCellName(0, 0), GetCellName(3, 0)).Merge(1);
array1[0, 0] = Name;
// column Name
for (int colnum = 0; colnum < manufacturedDt.Columns.Count; colnum++)
{
array1[1, colnum] = manufacturedDt.Columns[colnum].Caption;
}
// set value
for (int row = 2; row < manufacturedDt.Rows.Count + 2; row++)
{
for (int col = 0; col < manufacturedDt.Columns.Count; col++)
{
array1[row, col] = manufacturedDt.Rows[row - 2][col].ToString();
}
}
// input cell
_SRange.Value2 = array1;
app.Visible = true;
}
答案 0 :(得分:0)
解决了问题。 'String ARRAY'是原因。 String Array可能只导出excel'字符串类型',因此我无法应用单元格格式。 我更改了我的代码字符串[,]&gt;对象[,]。