DataSet数据集存入指定路径的Excel文件
如题,此方法将DataSet的数据存入到同一excel的同一个sheet中,不同的table之间用一个空行隔开。
1 /// <summary> 2 /// 将DataSet的数据存到指定路径的Excel文件的同一个sheet中,不同的table中间空一行 3 /// </summary> 4 /// <param name="dsExport">DataSet</param> 5 /// <param name="strFileName">Excel文件路径</param> 6 public static void DataTable2Excel(DataSet dsExport, string strFileName) 7 { 8 // Need an Excel App to save document. If Excel is not installed on the user computer, there will be an error message. 9 Excel.Application myExcelApp = new Excel.Application(); 10 if (myExcelApp == null) 11 { 12 MessageBox.Show("ERROR: EXCEL couldn't be started!"); 13 System.Windows.Forms.Application.Exit(); 14 } 15 16 // Let the App create a workbook and a worksheet in the workbook 17 18 Excel.Workbook myWorkbook = (Excel.Workbook)myExcelApp.Workbooks.Add(Type.Missing); 19 Excel.Style styHeader; styHeader = myWorkbook.Styles.Add("STYLE_HEADER", Type.Missing); 20 Excel.Style styText; 21 22 // styText = myWorkbook.Styles["STYLE_TEXT"]; 23 styText = myWorkbook.Styles.Add("STYLE_TEXT", Type.Missing); 24 25 //A new workbook contains 3 sheets by default, we need to clear all these blank sheet after adding a new one 26 while (myWorkbook.Sheets.Count > 1) 27 { 28 ((Excel.Worksheet)myWorkbook.Worksheets[1]).Delete(); 29 } 30 31 Excel.Worksheet myWorksheet = (Excel.Worksheet)myWorkbook.Worksheets.Add(Type.Missing, Type.Missing, 1, Type.Missing); 32 ((Excel.Worksheet)myWorkbook.Worksheets[1]).Name = "sheet1"; 33 34 // iHeaderOffset = 0 means NOT include the column names in the Excel worksheet 35 // iHeaderOffset = 1 means include the column names as the first line in the Excel worksheet 36 int iHeaderOffset = 0; 37 38 iHeaderOffset = 1; 39 DataTable dt; 40 int intRows = 0; 41 for (int m = 0; m < dsExport.Tables.Count; m++) 42 { 43 dt = dsExport.Tables[m]; 44 45 // Make the column names in DB the HEADER of the worksheet 46 for (int j = 0; j < dt.Columns.Count; j++) 47 { 48 Excel.Range rng1 = (Excel.Range)myWorksheet.Cells[1 + intRows + m, j + 1]; 49 rng1.Style = "STYLE_HEADER"; 50 myWorksheet.Cells[1 + intRows + m, j + 1] = dt.Columns[j].ColumnName; 51 } 52 intRows++; 53 54 #region styHeader 55 styHeader.Font.Name = "Arial"; 56 styHeader.Font.Bold = true; 57 styHeader.Font.Size = 10; 58 styHeader.Font.Color = ColorTranslator.ToOle(Color.White); 59 styHeader.Interior.Color = ColorTranslator.ToOle(Color.DarkBlue); 60 styHeader.Interior.Pattern = Excel.XlPattern.xlPatternSolid; 61 Excel.Range rng = myWorksheet.Range[myWorksheet.Cells[1, 1], myWorksheet.Cells[1, dt.Columns.Count]]; 62 rng.Style = "STYLE_HEADER"; 63 #endregion styHeader 64 65 66 #region styText 67 styText.Font.Bold = false; 68 styText.Font.Size = 10; 69 styText.Font.Color = ColorTranslator.ToOle(Color.Black); 70 styText.NumberFormat = "@"; 71 #endregion styText 72 73 for (int i = 0; i < dt.Rows.Count; i++) 74 { 75 for (int j = 0; j < dt.Columns.Count; j++) 76 { 77 Excel.Range rng1 = (Excel.Range)myWorksheet.Cells[intRows + m + iHeaderOffset, j + 1]; 78 rng1.Style = "STYLE_TEXT"; 79 myWorksheet.Cells[intRows + m + iHeaderOffset, j + 1] = dt.Rows[i][j].ToString().Trim(); 80 } 81 intRows++; 82 } 83 } 84 85 // Make the output looks good 86 myWorksheet.UsedRange.Columns.AutoFit(); 87 88 // Save the XLS file 89 myWorkbook.SaveAs(strFileName, // Filename 90 Excel.XlFileFormat.xlXMLSpreadsheet, // FileFormat 91 Type.Missing, // Password 92 Type.Missing, // WriteResPassword, 93 false, // ReadOnlyRecommended, 94 false, // CreateBackup, 95 Excel.XlSaveAsAccessMode.xlNoChange, // AccessMode, 96 Type.Missing, // ConflictResolution, 97 Type.Missing, // AddToMru, 98 Type.Missing, // TextCodepage, 99 Type.Missing, // TextVisualLayout, 100 Type.Missing // Object Local 101 ); 102 103 104 //Close the XLS file. 105 myWorkbook.Close(true, Type.Missing, Type.Missing); 106 myExcelApp.Quit(); 107 System.Runtime.InteropServices.Marshal.ReleaseComObject(myWorksheet); 108 System.Runtime.InteropServices.Marshal.ReleaseComObject(myWorkbook); 109 System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcelApp); 110 myWorksheet = null; 111 myWorkbook = null; 112 myExcelApp = null; 113 GC.Collect(); 114 115 }