![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public void ExpExcel(string fileName, DataTable dataTable)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Excel.ApplicationClass apc = new Excel.ApplicationClass();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
apc.Visible = false;
Excel.Workbook wkbook = apc.Workbooks.Add(true);
Excel.Worksheet wksheet = (Excel.Worksheet)wkbook.ActiveSheet;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int rowIndex = 2;
int colIndex = 1;
//设置第一行为文本
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[dataTable.Rows.Count, dataTable.Columns.Count]).NumberFormatLocal = "@";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//取得列标题
foreach (DataColumn dc in dataTable.Columns)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
wksheet.Cells[1, colIndex] = dc.ColumnName;
colIndex++;
}
colIndex = 1;
int rowCount = dataTable.Rows.Count;
int colCount = dataTable.Columns.Count;
//建立二维数组 将数据填充
object[,] dataArray = new object[rowCount, colCount];
for (int i = 0; i < rowCount; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for (int j = 0; j < colCount; j++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
dataArray[i, j] = dataTable.Rows[i][j];
}
}
//将二维数组的值赋给excel wksheet.Cells[rowIndex, colIndex] 起开始单元格 wksheet.Cells[rowCount + rowIndex-1, colCount + colIndex-1] 结束单元格
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
* 假如 开始单元格 wksheet.Cells[x, y] 结束单元格 wksheet.Cells[i,j] i-x 数据的行数 j-y 数据的列数
*/
wksheet.get_Range(wksheet.Cells[rowIndex, colIndex], wksheet.Cells[rowCount + rowIndex-1, colCount + colIndex-1]).Value2 = dataArray;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置表格样式 表头样式
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[1, dataTable.Columns.Count]).Interior.ColorIndex = 20;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[1, dataTable.Columns.Count]).Font.ColorIndex = 3;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[1, dataTable.Columns.Count]).Borders.Weight = Excel.XlBorderWeight.xlThin;
wksheet.get_Range(apc.Cells[1, 1], apc.Cells[dataTable.Rows.Count, dataTable.Columns.Count]).Columns.AutoFit();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (File.Exists(fileName))
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
File.Delete(fileName);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
wkbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
wkbook.Close(Type.Missing, Type.Missing, Type.Missing);
apc.Quit();
wkbook = null;
apc = null;
GC.Collect();
}
posted @
2008-08-02 15:45
啸翱姜糊
阅读(
236)
评论()
编辑
收藏
举报