public void ExportToExcel(HttpContext context, string reportName, bool Compress) { string xlsPath = AppPath.PhysicalPath(SiteUrls.Instance.Paths["ExcelTemplate"]); FileStream fs = new FileStream(xlsPath, FileMode.Open, FileAccess.Read); HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true); var style = templateWorkbook.CreateCellStyle(); SetCellStyle(style); HSSFSheet sheet = templateWorkbook.GetSheet("Sheet1"); //HSSFAnchor a sheet.DefaultColumnWidth = 20; int cellIndex = 1; const int startRow = 3; GenerateReportName(sheet, reportName); GenerateHeader(sheet, templateWorkbook, startRow, cellIndex); int i = 0; foreach (var listValue in _listValues) { cellIndex = 1; HSSFRow row = sheet.CreateRow(startRow + ++i); foreach (var o in listValue) { var cell = row.CreateCell(cellIndex++); if (o != null) { cell.SetCellValue(o.ToString()); } else { cell.SetCellValue(""); } cell.CellStyle = style; } } MemoryStream ms = new MemoryStream(); templateWorkbook.Write(ms); if (Compress) { ToCompress(context, templateWorkbook.GetBytes()); return; } //ToCompress(templateWorkbook.GetBytes()); DateTime d = DateTime.Now; string filename = string.Format("ExcelReport{0}_{1}_{2}_{3}_{4}.xls", d.Year, d.Month, d.Day, d.Hour, d.Minute); context.Response.AddHeader("Content-disposition", "attachment; filename=" + filename); context.Response.ContentType = "application/vnd.ms-excel"; context.Response.BinaryWrite(ms.GetBuffer()); context.Response.End(); }
public void TestSheetSerializeSizeMisMatch_bug45066() { HSSFWorkbook wb = new HSSFWorkbook(); InternalSheet sheet = ((HSSFSheet)wb.CreateSheet("Sheet1")).Sheet; IList sheetRecords = sheet.Records; // one way (of many) to cause the discrepancy is with a badly behaved record: sheetRecords.Add(new BadlyBehavedRecord()); // There is also much logic inside Sheet that (if buggy) might also cause the discrepancy try { wb.GetBytes(); throw new AssertionException("Identified bug 45066 a"); } catch (InvalidOperationException e) { // Expected badly behaved sheet record to cause exception Assert.IsTrue(e.Message.StartsWith("Actual serialized sheet size")); } }
public void Exportar(DataGrid dg) { try { itens = new List <object>((IEnumerable <object>)dg.ItemsSource); if (itens != null && itens.Count > 0) { Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.FileName = "dadosLista"; dlg.DefaultExt = ".xls"; dlg.Filter = "XLS documents (.xls)|*.xls"; Nullable <bool> result = dlg.ShowDialog(); if (result == true) { FileStream fs = new FileStream(dlg.FileName, FileMode.Create, FileAccess.ReadWrite); HSSFWorkbook workbook1 = new HSSFWorkbook(); Sheet planilha = workbook1.CreateSheet("default"); for (int i = 0; i < itens.Count + 1; i++) { planilha.CreateRow(i); } int indexColuna = 0; foreach (DataGridColumn coluna in dg.Columns) { Binding binding = (Binding)((DataGridBoundColumn)coluna).Binding; string path = binding.Path.Path; planilha.GetRow(0).CreateCell(indexColuna).SetCellValue(coluna.Header.ToString()); string[] hierarquiaObj = path.Split('.'); int indexLinhas = 1; dynamic conteudo = ""; foreach (Object item in itens) { Object itemAux = item; for (int i = 0; i < hierarquiaObj.Length - 1; i++) { itemAux = itemAux.GetType().GetProperty(hierarquiaObj[i]).GetValue(itemAux, null); } string prop = hierarquiaObj[hierarquiaObj.Length - 1]; conteudo = itemAux.GetType().GetProperty(prop).GetValue(itemAux, null).ToString(); planilha.GetRow(indexLinhas).CreateCell(indexColuna).SetCellValue(conteudo); indexLinhas++; } indexColuna++; } fs.Write(workbook1.GetBytes(), 0, workbook1.GetBytes().Length); fs.Flush(); fs.Close(); } } } catch (Exception ex) { throw ex; } }