OpenXmlSdk导出Excel
感觉OpenXmlSdk的语法真的不是很友好。研究了半天,只实现了简单的导出功能。对于单元格样式的设置暂时还是搞明白,网上的资料真的很少,官方文档是英文的。中文的文章大都是用工具(Open XML SDK 2.0 Productivity Tool)搞出来的,反正在我这是不管用。最终还是回到了NPOI 的怀抱。
最后还是把这点代码记录一下,以后有时间再继续研究吧。
1 using System; 2 using System.Data; 3 using System.IO; 4 using System.Web; 5 using DocumentFormat.OpenXml; 6 using DocumentFormat.OpenXml.Packaging; 7 using DocumentFormat.OpenXml.Spreadsheet; 8 9 public static class ExportHelper 10 { 11 /// <summary> 12 /// 导出Excel文件 13 /// </summary> 14 /// <param name="fileName"></param> 15 /// <param name="dataSet">DataSet中每个DataTable生成一个Sheet</param> 16 public static void ExportExcel(string fileName, DataSet dataSet) 17 { 18 if (dataSet.Tables.Count == 0) 19 { 20 return; 21 } 22 23 using (MemoryStream stream = DataTable2ExcelStream(dataSet)) 24 { 25 FileStream fs = new FileStream(fileName, FileMode.CreateNew); 26 stream.WriteTo(fs); 27 fs.Flush(); 28 fs.Close(); 29 } 30 } 31 32 public static void ExportExcel(string fileName, DataTable dataTable) 33 { 34 DataSet dataSet = new DataSet(); 35 dataSet.Tables.Add(dataTable); 36 ExportExcel(fileName, dataSet); 37 } 38 39 /// <summary> 40 /// Web导出Excel文件 41 /// </summary> 42 /// <param name="fileName"></param> 43 /// <param name="dataSet">DataSet中每个DataTable生成一个Sheet</param> 44 public static void ResponseExcel(string fileName, DataSet dataSet) 45 { 46 if (dataSet.Tables.Count == 0) 47 { 48 return; 49 } 50 51 using (MemoryStream stream = DataTable2ExcelStream(dataSet)) 52 { 53 ExportExcel(fileName, stream); 54 } 55 } 56 57 public static void ResponseExcel(string fileName, DataTable dataTable) 58 { 59 DataSet dataSet = new DataSet(); 60 dataSet.Tables.Add(dataTable.Copy()); 61 ResponseExcel(fileName, dataSet); 62 } 63 64 private static void ExportExcel(string fileName, MemoryStream stream) 65 { 66 HttpContext.Current.Response.Clear(); 67 HttpContext.Current.Response.Charset = "UTF-8"; 68 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename= " + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); 69 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; 70 HttpContext.Current.Response.ContentType = "application/ms-excel"; 71 HttpContext.Current.Response.BinaryWrite(stream.ToArray()); 72 HttpContext.Current.Response.Flush(); 73 HttpContext.Current.Response.End(); 74 } 75 76 private static MemoryStream DataTable2ExcelStream(DataSet dataSet) 77 { 78 MemoryStream stream = new MemoryStream(); 79 SpreadsheetDocument document = SpreadsheetDocument.Create(stream, 80 SpreadsheetDocumentType.Workbook); 81 82 WorkbookPart workbookPart = document.AddWorkbookPart(); 83 workbookPart.Workbook = new Workbook(); 84 85 Sheets sheets = document.WorkbookPart.Workbook.AppendChild(new Sheets()); 86 87 for (int i = 0; i < dataSet.Tables.Count; i++) 88 { 89 DataTable dataTable = dataSet.Tables[i]; 90 WorksheetPart worksheetPart = document.WorkbookPart.AddNewPart<WorksheetPart>(); 91 worksheetPart.Worksheet = new Worksheet(new SheetData()); 92 93 Sheet sheet = new Sheet 94 { 95 Id = document.WorkbookPart.GetIdOfPart(worksheetPart), 96 SheetId = (UInt32)(i + 1), 97 Name = dataTable.TableName 98 }; 99 sheets.Append(sheet); 100 101 SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); 102 103 Row headerRow = CreateHeaderRow(dataTable.Columns); 104 sheetData.Append(headerRow); 105 106 for (int j = 0; j < dataTable.Rows.Count; j++) 107 { 108 sheetData.Append(CreateRow(dataTable.Rows[j], j + 2)); 109 } 110 } 111 112 document.Close(); 113 114 return stream; 115 } 116 117 private static Row CreateHeaderRow(DataColumnCollection columns) 118 { 119 Row header = new Row(); 120 for (int i = 0; i < columns.Count; i++) 121 { 122 Cell cell = CreateCell(i + 1, 1, columns[i].ColumnName, CellValues.String); 123 header.Append(cell); 124 } 125 return header; 126 } 127 128 private static Row CreateRow(DataRow dataRow, int rowIndex) 129 { 130 Row row = new Row(); 131 for (int i = 0; i < dataRow.Table.Columns.Count; i++) 132 { 133 Cell cell = CreateCell(i + 1, rowIndex, dataRow[i], GetType(dataRow.Table.Columns[i].DataType)); 134 row.Append(cell); 135 } 136 return row; 137 } 138 139 private static CellValues GetType(Type type) 140 { 141 if (type == typeof(decimal)) 142 { 143 return CellValues.Number; 144 } 145 //if ((type == typeof(DateTime))) 146 //{ 147 // return CellValues.Date; 148 //} 149 return CellValues.SharedString; 150 } 151 152 private static Cell CreateCell(int columnIndex, int rowIndex, object cellValue, CellValues cellValues) 153 { 154 Cell cell = new Cell 155 { 156 CellReference = GetCellReference(columnIndex) + rowIndex, 157 CellValue = new CellValue { Text = cellValue.ToString() }, 158 DataType = new EnumValue<CellValues>(cellValues), 159 StyleIndex = 0 160 }; 161 return cell; 162 } 163 164 private static string GetCellReference(int colIndex) 165 { 166 int dividend = colIndex; 167 string columnName = String.Empty; 168 169 while (dividend > 0) 170 { 171 int modifier = (dividend - 1) % 26; 172 columnName = Convert.ToChar(65 + modifier) + columnName; 173 dividend = (dividend - modifier) / 26; 174 } 175 176 return columnName; 177 } 178 }