问题
I need to save a report to MemoryStream
, so I do the next:
byte[] result = new byte[0];
using (var stream = new MemoryStream())
{
XlsxReport.BuildTaskReport(stream);
result = stream.ToArray();
}
Below my XlsxReport
class:
public sealed class XlsxReport
{
public const string FontName = "Times New Roman";
private const int FontSize = 14;
private readonly IWorkbook workbook;
// Fonts
private readonly IFont boldFont;
private readonly IFont defaultFont;
// Styles
private readonly ICellStyle cbStyle;
private readonly ICellStyle simpleStyle;
private readonly ICellStyle cdStyle;
private readonly ICellStyle ldStyle;
// Sheet
private readonly ISheet sheet;
private readonly int rows, columns;
private XlsxReport(int rows, int columns)
{
this.rows = rows;
this.columns = columns;
workbook = new XSSFWorkbook();
// Prepare fonts
boldFont = workbook.CreateFont();
defaultFont = workbook.CreateFont();
boldFont.FontHeightInPoints = FontSize;
defaultFont.FontHeightInPoints = FontSize;
boldFont.FontName = FontName;
defaultFont.FontName = FontName;
boldFont.Boldweight = (short)FontBoldWeight.Bold;
// Prepare styles
cbStyle = CreateCellStyle(boldFont);
simpleStyle = CreateCellStyle(defaultFont, HorizontalAlignment.Justify);
cdStyle = CreateCellStyle(defaultFont, HorizontalAlignment.Center, VerticalAlignment.Center, true, false);
ldStyle = CreateCellStyle(defaultFont, HorizontalAlignment.Left, VerticalAlignment.Center, true, false);
// Initialize a sheet
sheet = workbook.CreateSheet(Headers.Page + " 1");
CreateSheet(sheet, rows, columns, cdStyle);
}
private ICellStyle CreateCellStyle(IFont font, HorizontalAlignment horizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment verticalAlignment = VerticalAlignment.Center, bool hasBorder = true, bool wrapText = true)
{
var style = workbook.CreateCellStyle();
style.SetFont(font);
style.Alignment = horizontalAlignment;
style.VerticalAlignment = verticalAlignment;
style.WrapText = wrapText;
if (hasBorder)
{
SetBorder(style);
}
return style;
}
private void BuildMainHeader()
{
// Prepare styles
var titleStyle = CreateCellStyle(boldFont, HorizontalAlignment.Center, VerticalAlignment.Center, false, false);
var stampStyle = CreateCellStyle(defaultFont, HorizontalAlignment.Center, VerticalAlignment.Center, false, false);
// Create a title
GetCell(sheet.GetRow(0), 0, Headers.Report, titleStyle);
// Create a timestamp
GetCell(sheet.GetRow(2), 0, DateTime.Today.ToDateStr(), stampStyle);
// Set regions
var titleRegion = new CellRangeAddress(0, 0, 0, columns - 1);
var stampRegion = new CellRangeAddress(2, 2, 0, columns - 1);
sheet.AddMergedRegion(titleRegion);
sheet.AddMergedRegion(stampRegion);
}
private void SetBorder(ICellStyle style)
{
style.BorderTop = BorderStyle.Medium;
style.BorderBottom = BorderStyle.Medium;
style.BorderLeft = BorderStyle.Medium;
style.BorderRight = BorderStyle.Medium;
}
private ICell GetCell(IRow row, int index, string value, ICellStyle style)
{
var cell = row.GetCell(index);
cell.SetCellValue(value);
cell.CellStyle = style;
cell.SetCellType(CellType.String);
return cell;
}
private void SetBorder(CellRangeAddress region, ISheet sheet, IWorkbook wb)
{
RegionUtil.SetBorderBottom(2, region, sheet, wb);
RegionUtil.SetBorderTop(2, region, sheet, wb);
RegionUtil.SetBorderLeft(2, region, sheet, wb);
RegionUtil.SetBorderRight(2, region, sheet, wb);
}
private void CreateSheet(ISheet sheet, int rows, int columns, ICellStyle style)
{
for (int i = 0; i < rows; ++i)
{
var row = sheet.CreateRow(i);
for (int j = 0; j < columns; ++j)
{
var cell = row.CreateCell(j);
if (i > 3) { cell.CellStyle = style; }
}
}
}
private void AutoSizeColumns()
{
for (int i = 0; i < columns; ++i)
{
sheet.AutoSizeColumn(i);
}
}
public static void BuildTaskReport(Stream stream)
{
var report = new XlsxReport(5 + Count * 6, 6);
report.BuildMainHeader();
var sheet = report.sheet;
// Set cell regions
var head = report.sheet.GetRow(4);
report.GetCell(head, 0, Headers.OrderNumber, report.cbStyle);
report.GetCell(head, 1, Headers.TaskName, report.cbStyle);
report.GetCell(head, 2, Headers.DateCreate, report.cbStyle);
report.GetCell(head, 3, Headers.DateEnd, report.cbStyle);
report.GetCell(head, 4, Headers.Priority, report.cbStyle);
report.GetCell(head, 5, Headers.Status, report.cbStyle);
////////////////////////////////////////////////////////
// Many lines to fill rows
////////////////////////////////////////////////////////
// Resize columns
report.AutoSizeColumns();
// Save changes
report.workbook.Write(stream);
stream.Flush();
}
}
But after using
block my result
is still 0 bytes. I have checked in the debugger my report builder: IWorkbook
contains a sheet and the sheet contains rows, all looks right and seems no any exceptions.
What can be a problem? It works before I have moved a code to XlsxReport
class.
回答1:
I run into the same problem today. The problem was that I updated SharpZipLib library from 0.86.* to 1.0.*. And it stopped working, or at least application started to put 0 bytes into the stream. When I downgraded this library back to 0.86.* everything started to work again as it is expected.
来源:https://stackoverflow.com/questions/52432909/npoi-writes-0-bytes-in-the-memorystream