Apache POI - How to write to XLS in parts - apache-poi

I use Apache POI and I have a lot of data that I want to write to *.xls file in parts.
Now I use:
FileOutputStream fileOutputStream = null;
File tmpFile = new File("blabla.xls");
Workbook workbook = null;
try {
for(int i = 1; i <= pageNumber; i++) {
workbook = xlsGenerator.generateWorkbook(someData, i);
fileOutputStream = new FileOutputStream(tmpFile);
workbook.write(fileOutputStream);
}
}
But it doesn't work. It always replace old data instead of appending new data to workbook. So, are there ways to write in parts?

Related

Convert .xlsx file to html using NPOI

I want to convert .xlsx file to html using NPOI. Is this possible? I know , xls to html conversion is available using NPOI. But not sure if NPOI provide option to convert .xlsx file to html also. Thanks
You can use ExcelToHtmlConverter. It has method ProcessWorkbook which accepts IWorkbook as a parameter. So it can be used to convert either HSSFWorkbook (xls) or XSSFWorkbook (xlsx).
public void ConvertXlsxToHtml()
{
XSSFWorkbook xssfwb;
var fileName = #"c:\temp\test.xlsx";
using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
xssfwb = new XSSFWorkbook(file);
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter();
//set output parameter
excelToHtmlConverter.OutputColumnHeaders = false;
excelToHtmlConverter.OutputHiddenColumns = true;
excelToHtmlConverter.OutputHiddenRows = true;
excelToHtmlConverter.OutputLeadingSpacesAsNonBreaking = false;
excelToHtmlConverter.OutputRowNumbers = false;
excelToHtmlConverter.UseDivsToSpan = true;
//process the excel file
excelToHtmlConverter.ProcessWorkbook(xssfwb);
//output the html file
excelToHtmlConverter.Document.Save(Path.ChangeExtension(fileName, "html"));
}
}

The file format and extension of <filename>.xls don't match -- apache-poi 4.1.2

I am creating xls with apache-poi version 4.1.2. when I do with junit it is working correctly but if I use tomee with container then the generated file is showing an error like
The file format and extension of <filename>.xls don't match
Java version 1.8
tomee-plus 1.7.5
K8s docker container
Using HSSFWorkbook
File outputFile = new File(dir, fileParameter.getFileName());
if (outputFile.exists() && !FileUtils.deleteFile(outputFile)) {
LOG.warn("Cannot delete");
}
try (final HSSFWorkbook workbook = new HSSFWorkbook()) {
final HSSFSheet sheet = workbook.createSheet(SUMMARY_SHEET_NAME);
//Header Row
int rowNum = 0;
int cellNum = 0;
HSSFRow row = sheet.createRow(rowNum++);
row.createCell(cellNum++).setCellValue("TEST1");
row.createCell(cellNum++).setCellValue("TEST2");
row.createCell(cellNum++).setCellValue("TEST3");
row.createCell(cellNum++).setCellValue("TEST4");
row.createCell(cellNum++).setCellValue("TEST5");
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
workbook.write(fileOutputStream);
}
}

Not able to read excel sheet saved as xls using closedxml

I have the below code to save the data in excel sheet as .xls
public ActionResult ExportToExcel()
{
DataTable tbl = CopyGenericToDataTable(res);
tbl.TableName = "InvalidInvoices";
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(tbl);
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename= "+fileName + ".xls");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
}
Above is code which download the xls excel sheet at client side. It works fine the data gets saved in excel sheet.
Problem is if I try to upload this same file using below code -
if (files != null)
{
HttpPostedFileBase upload = files.FirstOrDefault();
Stream stream = upload.InputStream;
DataSet result = new DataSet();
if (upload != null && upload.ContentLength > 0)
{
if (upload.FileName.EndsWith(".xls") || upload.FileName.EndsWith(".xlsx"))
{
// ExcelDataReader works with the binary Excel file, so it needs a FileStream
// to get started. This is how we avoid dependencies on ACE or Interop:
// We return the interface, so that
IExcelDataReader reader = null;
if (upload.FileName.EndsWith(".xls"))
{
reader = ExcelReaderFactory.CreateBinaryReader(stream);
}
else if (upload.FileName.EndsWith(".xlsx"))
{
reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
}
reader.IsFirstRowAsColumnNames = false;
result = reader.AsDataSet();
reader.Close();
}
}
}
In above code I am getting error in ExcelReaderFactory.CreateBinaryReader(stream);
In stream it has the values in bytes too just on using createBinaryreader of excelreaderfactory reader has error message as 'Invalid file signature'.
Any help will be highly appreciated.
ClosedXML generates .xlsx files, not .xls files.
Check your code:
Response.AddHeader("content-disposition", "attachment;filename= "+fileName + ".xls");

ClosedXML generating malformatted xlsx

Can't seem to output a good file with ClosedXML, when I open the file I'm getting
We found a problem with some content in 'filename.xlsx' Do you want us to recover as much as we can? If you trust the source of this workbook, click Yes.
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.AddWorksheet("name");
worksheet.Row(1).Cell(i + 1).SetValue("test");
worksheet.Row(k + 2).Cell(column.Order + 1).SetValue("test value");
using (var memoryStream = new MemoryStream())
{
workbook.SaveAs(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
return memoryStream.GetBuffer();
}
}
It was either using ToArray() instead of GetBuffer() that fixed the problem. Cheers
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.AddWorksheet("name");
worksheet.Row(1).Cell(i + 1).SetValue("test");
worksheet.Row(k + 2).Cell(column.Order + 1).SetValue("test value");
using (var memoryStream = new MemoryStream())
{
workbook.SaveAs(memoryStream);
memoryStream.Position = 0;
return memoryStream.ToArray();
}
}

XLSX removing sheets OutOfMemory Exception

I am trying to load the XLSX file using POI library that has 5 sheets. Size of the file is 5 MB. Total records in all sheets are around 30,000.
Once the file is loaded i need to delete the 1 or more sheets on the fly based on sheet neame as input.
Here is the snippet.
public void generateReportWorkBook(String[] requestedReports) throws Exception {
// Read the file
String dailyTicketReport = ReportConstants.REPORT_PATH + ReportConstants.FILE_NAME + ReportConstants.XLSX_FILE_EXTN;
FileInputStream fis = null;
XSSFWorkbook book = null;
try {
fis = new FileInputStream(dailyTicketReport);
book = new XSSFWorkbook(fis);
for (int i = book.getNumberOfSheets() - 1; i >= 0; i--) {
XSSFSheet tmpSheet = book.getSheetAt(i);
if (!ArrayUtils.contains(requestedReports, tmpSheet.getSheetName())) {
book.removeSheetAt(i);
}
}
} catch (Exception e) {
logger.error("Error occured while removing the sheets from workbook");
throw e;
} finally {
IOUtils.closeQuietly(fis);
}
}
When i execute the program. I get OutofMemory Exception.
How can i remove the sheets without memory issue.
I too faced the same issue of OOM while parsing xlsx file...after two days of struggle, I finally found out the below code that was really perfect;
This code is based on sjxlsx. It reads the xlsx and stores in a HSSF sheet.
// read the xlsx file
SimpleXLSXWorkbook = new SimpleXLSXWorkbook(new File("C:/test.xlsx"));
HSSFWorkbook hsfWorkbook = new HSSFWorkbook();
org.apache.poi.ss.usermodel.Sheet hsfSheet = hsfWorkbook.createSheet();
Sheet sheetToRead = workbook.getSheet(0, false);
SheetRowReader reader = sheetToRead.newReader();
Cell[] row;
int rowPos = 0;
while ((row = reader.readRow()) != null) {
org.apache.poi.ss.usermodel.Row hfsRow = hsfSheet.createRow(rowPos);
int cellPos = 0;
for (Cell cell : row) {
if(cell != null){
org.apache.poi.ss.usermodel.Cell hfsCell = hfsRow.createCell(cellPos);
hfsCell.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
hfsCell.setCellValue(cell.getValue());
}
cellPos++;
}
rowPos++;
}
return hsfSheet;

Resources