NET导出EXCL 列头合并了,帮忙去掉合并的代码
public static void DataTabletoExcel(DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
return;
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
Microsoft.Office.Interop.Excel.ApplicationClass MyExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
MyExcel.Visible = false;//excel是否可见
MyExcel.DisplayAlerts = false;//屏蔽一些弹出窗口
Microsoft.Office.Interop.Excel.Workbooks MyWorkBooks = MyExcel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook MyWorkBook = MyWorkBooks.Add(System.Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet MyWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)MyWorkBook.Worksheets[1];
//ar xlApp = new Application();
//xlApp.DefaultFilePath = "";
//xlApp.DisplayAlerts = true;
//xlApp.SheetsInNewWorkbook = 1;
//Workbook xlBook = xlApp.Workbooks.Add(true);
//MyWorkSheet.Cells[1, 1] = projectName + " 进度计划";
//导入列名
for (int i = 0; i < columnNum; i++)
{
MyWorkSheet.Cells[1, i + 1] = tmpDataTable.Columns[i].ColumnName;
}
//将DataTable中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < columnNum; j++)
{
MyWorkSheet.Cells[i + 2, j + 1] = tmpDataTable.Rows[i][j].ToString();
}
}
MyExcel.Application.DisplayAlerts = false;
Microsoft.Office.Interop.Excel.Range RangeTitle = MyWorkSheet.get_Range((Microsoft.Office.Interop.Excel.Range)MyWorkSheet.Cells[1, 1], (Microsoft.Office.Interop.Excel.Range)MyWorkSheet.Cells[1, Convert.ToInt16(columnNum)]);
RangeTitle.Merge(false);
RangeTitle.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
RangeTitle.Interior.ColorIndex = 34;
MyExcel.Application.DisplayAlerts = true;
Microsoft.Office.Interop.Excel.Range Range1 = MyWorkSheet.get_Range((Microsoft.Office.Interop.Excel.Range)MyWorkSheet.Cells[1, 1], (Microsoft.Office.Interop.Excel.Range)MyWorkSheet.Cells[rowNum + 2, Convert.ToInt16(columnNum)]);
Range1.Font.Name = "微软雅黑";
Range1.EntireColumn.AutoFit();
Range1.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null);
Range1.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;//块内竖线
Range1.Borders.get_Item(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;//块内横线
//MyWorkSheet.get_Range((Range)MyWorkSheet.Cells[1, 1],(Range)MyWorkSheet.Cells[1, Convert.ToInt16(columnNum + cengValue - 1)]).Interior.ColorIndex = 16;
//MyWorkSheet.get_Range((Range)MyWorkSheet.Cells[1, 1], (Range)MyWorkSheet.Cells[10000, 100]).EntireColumn.AutoFit(); //列宽自适应
SPSecurity.RunWithElevatedPrivileges(delegate { MyWorkBook.SaveCopyAs(strFileName); });
}