C# 操作Excel多个sheet的具体的操作是什么呢?让我们来看看下面的实例实现:
- private void DataViewExcelBySheetMultipleDt(
- DataSet ds, string fileName)
- {
- try
- {
- int sheetCount = ds.Tables.Count;
- GC.Collect();
- Application excel;
- _Workbook xBk;
- _Worksheet xSt = null;
- excel = new ApplicationClass();
- xBk = excel.Workbooks.Add(true);
- int rowIndex = 0;
- int colIndex = 0;
- for (int sheetIndex = 0;
- sheetIndex < sheetCount; sheetIndex++)
- {
- rowIndex = 1;
- colIndex = 1;
- xSt = (_Worksheet)xBk.Worksheets.Add(
- Type.Missing, Type.Missing, 1, Type.Missing);
- switch (sheetIndex)
- {
- case 0:
- xSt.Name = "test1";
- break;
- case 1:
- xSt.Name = "test2";
- break;
- case 2:
- xSt.Name = "test3";
- break;
- case 3:
- xSt.Name = "test4";
- break;
- } //C# 操作Excel多个sheet的具体的操作
- foreach (DataColumn col in ds.Tables[sheetIndex].Columns)
- {
- xSt.get_Range(excel.Cells[rowIndex,
- colIndex], excel.Cells[rowIndex,
- colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
- xSt.get_Range(excel.Cells[rowIndex,
- colIndex], excel.Cells[rowIndex,
- colIndex]).Font.Bold = true;
- excel.Cells[rowIndex, colIndex++] = col.ColumnName;
- }
- foreach (DataRow row in ds.
- Tables[sheetIndex].Rows)
- {
- rowIndex++;
- colIndex = 1;
- foreach (DataColumn col in ds.
- Tables[sheetIndex].Columns)
- {
- if (col.DataType == System.Type.GetType(
- "System.DateTime"))
- {
- if (!"".Equals(row[col.ColumnName].ToString()))
- excel.Cells[rowIndex, colIndex] =
- (Convert.ToDateTime(row[col.ColumnName].
- ToString())).ToString("MM/dd/yyyy");
- else
- excel.Cells[rowIndex, colIndex] = "";
- }
- else if (col.DataType == S
- ystem.Type.GetType("System.String"))
- {
- excel.Cells[rowIndex, colIndex] = "'" +
- row[col.ColumnName].ToString();
- }
- else
- {
- excel.Cells[rowIndex, colIndex] =
- row[col.ColumnName].ToString();
- }
- colIndex++;
- } //C# 操作Excel多个sheet的具体的操作
- }
- Range allDataWithTitleRange = xSt.get_Range(
- excel.Cells[1, 1], excel.Cells[rowIndex, colIndex - 1]);
- allDataWithTitleRange.Select();
- allDataWithTitleRange.Columns.AutoFit();
- allDataWithTitleRange.Borders.LineStyle = 1;
- }
- string exportDir = "~/Attachment/";
- string absFileName = HttpContext.
- Current.Server.MapPath(
- System.IO.Path.Combine(exportDir, fileName));
- xBk.SaveCopyAs(absFileName);
- xBk.Close(false, null, null);
- excel.Quit();
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(xBk);
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(excel);
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(xSt);
- //C# 操作Excel多个sheet的具体的操作
- xBk = null;
- excel = null;
- xSt = null;
- GC.Collect();
- }
- catch (Exception ex)
- {
- }
- }
- private void DataViewExcelBySheetMultipleDt(
- DataSet ds, string fileName)
- {
- try
- {
- int sheetCount = ds.Tables.Count;
- GC.Collect();
- Application excel;
- _Workbook xBk;
- _Worksheet xSt = null;
- excel = new ApplicationClass();
- xBk = excel.Workbooks.Add(true);
- //C# 操作Excel多个sheet的具体的操作
- int rowIndex = 0;
- int colIndex = 0;
- for (int sheetIndex = 0;
- sheetIndex < sheetCount; sheetIndex++)
- {
- rowIndex = 1;
- colIndex = 1;
- xSt = (_Worksheet)xBk.Worksheets.Add(
- Type.Missing, Type.Missing, 1, Type.Missing);
- switch (sheetIndex)
- {
- case 0:
- xSt.Name = "test1";
- break;
- case 1:
- xSt.Name = "test2";
- break;
- case 2:
- xSt.Name = "test3";
- break;
- case 3:
- xSt.Name = "test4";
- break;
- }
- foreach (DataColumn col in ds.Tables[sheetIndex].Columns)
- { //C# 操作Excel多个sheet的具体的操作
- xSt.get_Range(excel.Cells[rowIndex,
- colIndex], excel.Cells[rowIndex,
- colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
- xSt.get_Range(excel.Cells[rowIndex,
- colIndex], excel.Cells[rowIndex, colIndex]).Font.Bold = true;
- excel.Cells[rowIndex, colIndex++] = col.ColumnName;
- }
- foreach (DataRow row in ds.Tables[sheetIndex].Rows)
- {
- rowIndex++;
- colIndex = 1;
- foreach (DataColumn col in ds.Tables[
- sheetIndex].Columns)
- {
- if (col.DataType == System.Type.GetType(
- "System.DateTime"))
- {
- if (!"".Equals(row[col.ColumnName].ToString()))
- excel.Cells[rowIndex, colIndex] = (
- Convert.ToDateTime(row[col.ColumnName].
- ToString())).ToString("MM/dd/yyyy");
- else
- excel.Cells[rowIndex, colIndex] = "";
- }
- else if (col.DataType ==
- System.Type.GetType("System.String"))
- {
- excel.Cells[rowIndex,
- colIndex] = "'" + row[col.ColumnName].ToString();
- }
- else
- {
- excel.Cells[rowIndex,
- colIndex] = row[col.ColumnName].ToString();
- }
- colIndex++;
- }
- } //C# 操作Excel多个sheet的具体的操作
- Range allDataWithTitleRange = xSt.get_Range(
- excel.Cells[1, 1],
- excel.Cells[rowIndex, colIndex - 1]);
- allDataWithTitleRange.Select();
- allDataWithTitleRange.Columns.AutoFit();
- allDataWithTitleRange.Borders.LineStyle = 1;
- }
- string exportDir = "~/Attachment/";
- string absFileName = HttpContext.Current.Server.
- MapPath(System.IO.Path.Combine(exportDir, fileName));
- xBk.SaveCopyAs(absFileName);
- xBk.Close(false, null, null);
- excel.Quit();
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(xBk);
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(excel);
- System.Runtime.InteropServices.
- Marshal.ReleaseComObject(xSt);
- xBk = null;
- excel = null;
- xSt = null;
- GC.Collect();
- }
- catch (Exception ex)
- {
- }
- }
上面方法,首先形成一个多个DataTable的DataSet,
C# 操作Excel重点还是
1. 生成一个新的xls时,打开方式,总是会提示进程占用
2. 用不同的sheet时一定要命名
3. 使用传入一个datatable时,总是会重写第一个sheet
C# 操作Excel多个sheet的具体的操作实例就向你介绍到这里,希望对你了解和学习C# 操作Excel多个sheet的具体的操作有所帮助。
【编辑推荐】