// TODO: Move to OOXML public static void Export(this IEnumerable data, Stream stream, List<ExportColumnModel> colModel) { var workbook = new HSSFWorkbook(); ////create a entry of DocumentSummaryInformation var dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "Higgs RIA Framework"; workbook.DocumentSummaryInformation = dsi; ////create a entry of SummaryInformation var si = PropertySetFactory.CreateSummaryInformation(); si.Subject = "JqGrid Export Data"; workbook.SummaryInformation = si; var sheet = workbook.CreateSheet("Export-Data"); var count = 1; var firstRow = sheet.CreateRow(0); for(var i = 0;i < colModel.Count;i++) { firstRow .CreateCell(i) .SetCellValue(colModel[i].Title); } sheet.CreateFreezePane(0, 1); Dictionary<string, PropertyInfo> properties = null; foreach (var item in data) { if (properties == null) { properties = item.GetType() .GetProperties() .ToDictionary(pi => pi.Name); } var row = sheet.CreateRow(count++); for (var i = 0; i < colModel.Count; i++) { var cell = row.CreateCell(i); if(!properties.ContainsKey(colModel[i].Name)) continue; var cellValue = properties[colModel[i].Name].GetValue(item, null); workbook.SetCellValue(cell, cellValue); } } for (var i = 0; i < colModel.Count; i++) { sheet.AutoSizeColumn(i); } workbook.Write(stream); }
public static void Export(this DataTable data, Stream stream) { var workbook = new HSSFWorkbook(); ////create a entry of DocumentSummaryInformation var dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "Higgs RIA Framework"; workbook.DocumentSummaryInformation = dsi; ////create a entry of SummaryInformation var si = PropertySetFactory.CreateSummaryInformation(); si.Subject = "JqGrid Export Data"; workbook.SummaryInformation = si; var sheet = workbook.CreateSheet("Export-Data"); var count = 1; var firstRow = sheet.CreateRow(0); for (var i = 0; i < data.Columns.Count; i++) { firstRow .CreateCell(i) .SetCellValue(data.Columns[i].ColumnName); } sheet.CreateFreezePane(0, 1); foreach (DataRow r in data.Rows) { var row = sheet.CreateRow(count++); for (var i = 0; i < data.Columns.Count; i++) { var cell = row.CreateCell(i); var cellValue = r[i]; workbook.SetCellValue(cell, cellValue); } } for (var i = 0; i < data.Columns.Count; i++) { sheet.AutoSizeColumn(i); } workbook.Write(stream); }