62,266
社区成员
发帖
与我相关
我的任务
分享 GC.Collect();
Excel.Application excel = new Excel.ApplicationClass();
Excel._Workbook xBk;
Excel._Worksheet xSt;
xBk = excel.Workbooks.Add(true);
xSt = (Excel._Worksheet)xBk.ActiveSheet;
string xTitle = "";
int rowIndex; //行
int colIndex; //列
int colcnt; //列数
int rowcnt; //行数
int Mgcnt; //合并行数
object Cell1, Cell2; //合并范围
xSt.Cells.Font.Size = 10;
xSt.Cells.Font.Name = "宋体";
xSt.Cells.NumberFormatLocal = "@"; //文本格式
xSt.Rows.WrapText = true; //自动换行
colcnt = dt.Columns.Count;
rowIndex = 1;
colIndex = 1;
rowcnt = dt.Rows.Count;
for (int i = 0; i < colcnt; i++)
{
excel.Cells[rowIndex, i + 1] = "'" + dt.Columns[i].ColumnName;
}
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, Cells]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
//行高
//xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, Cells]).RowHeight = 40;
xSt.get_Range(excel.Cells[1, 1], excel.Cells[1, Cells]).Cells.Interior.ColorIndex = 40;
for (int i = 1; i <= Cells; i++)
{
//列宽度
xSt.get_Range(excel.Cells[1, i], excel.Cells[1, i]).ColumnWidth = CellWidth;// CellWidth[i - 1];
}
xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowcnt + 1, Cells]).Borders.LineStyle = 1;