/// <summary> /// 通过DataTable向一个现有的excel表中增加一个sheet /// </summary> /// <param name="dt">带有数据集的DataTable</param> /// <param name="toFileName">现有excel的路径以及名称</param> /// <param name="strSheetName">需要添加的Sheet的名字</param> private void doExport(DataTable dt, string toFileName, string strSheetName) { Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); //Execl的操作类 //读取保存目标的对象 Microsoft.Office.Interop.Excel.Workbook bookDest = excel.Workbooks._Open(toFileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); //打开要导出到的Execl文件的工作薄。--ps:关于Missing类在这里的作用,我也不知道...囧 Microsoft.Office.Interop.Excel.Worksheet sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Microsoft.Office.Interop.Excel.Worksheet; //给工作薄添加一个Sheet sheetDest.Name = strSheetName;//自己定义名字O(∩_∩)O哈哈~ int rowIndex = 1; int colIndex = 0; excel.Application.Workbooks.Add(true);//这句不写不知道会不会报错 Microsoft.Office.Interop.Excel.Range range = null; foreach (DataColumn col in dt.Columns) { colIndex++; sheetDest.Cells[1, colIndex] = col.ColumnName;//Execl中的第一列,把DataTable的列名先导进去 sheetDest.get_Range(sheetDest.Cells[1, 1], sheetDest.Cells[1, dt.Columns.Count]).HorizontalAlignment = Microsoft.Office.Core.XlHAlign.xlHAlignCenterAcrossSelection; range = (Microsoft.Office.Interop.Excel.Range)sheetDest.Cells[1, colIndex]; range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); range.Font.Bold = true; //粗体 } #region //导入数据行 //foreach (DataRow row in dt.Rows) //{ // rowIndex++; // colIndex = 0; // foreach (DataColumn col in dt.Columns) // { // colIndex++; // sheetDest.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString(); // } //} #endregion for (int i = 0; i < dt.Rows.Count; i++) { DataRow row = dt.Rows[i]; for (int j = 0; j < dt.Columns.Count; j++) { DataColumn col = dt.Columns[j]; sheetDest.Cells[i + 2, j + 1] = row[col.ColumnName].ToString(); range = (Microsoft.Office.Interop.Excel.Range)sheetDest.Cells[i + 2, j + 1]; range.BorderAround(Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous, Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin, Microsoft.Office.Interop.Excel.XlColorIndex.xlColorIndexAutomatic, null); } } sheetDest.Columns.EntireColumn.AutoFit();//列宽自适应 if (toFileName.ToLower().Contains("wish")) { sheetDest.get_Range(sheetDest.Cells[1, 1], sheetDest.Cells[dt.Rows.Count + 1, dt.Columns.Count - 5]).HorizontalAlignment = Microsoft.Office.Core.XlHAlign.xlHAlignCenterAcrossSelection; } else { sheetDest.get_Range(sheetDest.Cells[1, 1], sheetDest.Cells[dt.Rows.Count + 1, dt.Columns.Count - 4]).HorizontalAlignment = Microsoft.Office.Core.XlHAlign.xlHAlignCenterAcrossSelection; } bookDest.Saved = true; bookDest.Save(); excel.Quit(); excel = null; GC.Collect();//垃圾回收 }
时间: 2024-11-12 17:04:24