private void button6_Click(object sender, System.EventArgs e)
{
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbooks workbooks = excel.Workbooks;
Excel._Workbook workbook = workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Sheets sheets = workbook.Worksheets;
Excel._Worksheet worksheet = (Excel._Worksheet)sheets.get_Item(1);
int rowIndex=1;
int colIndex=1;
DataTable table= dt;
int row_cnt = dt.Rows.Count;
int col_cnt = dataGridResult.TableStyles[0].GridColumnStyles.Count;
// 适用于用DataGridTableStyle自定义DataGrid时。
for(int j = 0;j < col_cnt; j++)
{
excel.Cells[rowIndex,j + 1]= dataGridResult.TableStyles[0].GridColumnStyles[j].HeaderText;
}
rowIndex++;
//同样方法处理数据
for(int row = 0; row < row_cnt; row++)
{
colIndex=1;
for(int col = 0; col < col_cnt; col++)
{
excel.Cells[rowIndex,colIndex] = dataGridResult[row, col];
colIndex++;
}
rowIndex++;
}
//设置单元格数字内容显示格式
Excel.Range range;
range = worksheet.get_Range(worksheet.Cells[2,1], worksheet.Cells[row_cnt + 1, 1]);
range.NumberFormat = "0000"; //这里是因为需要把0012等在Excel上正确显示,默认会变成12
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
range= worksheet.get_Range(worksheet.Cells[1,1],worksheet.Cells[row_cnt + 1,col_cnt]);
// 内框及外框
range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
excel.Columns.AutoFit();
excel.Visible=true;
}
}
}
我有一段DATAGRID导入EXCEL的代码,但是有错误请大家帮助!!,错误提示是说我没有Excel.ApplicationClass excel,请问我要怎以做啊