Multipart to xssfworkbook - excel

I am constructing an API that takes an Excel file as multipart, but in the implementation I need to read data from this multipart file, so I am using the xssfworkbook class. Can I convert the accepted .xlsx multipart file to xssfworkbook in Java?
fileAutomate(#RequestBody MultipartFile file)
Workbook workbook = new XSSFWorkbook(file)
List myData=excelTesting.getDataFromSheet(workbook,'Properties')

Related

Not to read hidden sheets of xlsb file using XSSFBReader in apache poi

I am reading xlsb file using XSSFBReader class from apache poi library. I do not find any way to read only visible sheets of xlsb file. Currently, code reads all the sheets of xlsb file.
How to read only visible sheets using XSSFBReader ?
pkg = OPCPackage.open(filename, PackageAccess.READ_WRITE);
XSSFBReader r = new XSSFBReader(pkg);
XSSFBReader.SheetIterator it = (XSSFBReader.SheetIterator) r.getSheetsData();
while (it.hasNext()) { //This iterates over all sheets of xlsb file
InputStream is = it.next();
String sheetName = it.getSheetName();
//some processing
}

Convert file directly to XLSX format using Spire.XLS

I have XLS file with macro, to convert it to OpenXML format, I am using Spire.XLS and able to convert file to XLSM format. Then using OpenXML SDK to convert XLSM file to XLSX format like below:
string lstrOpenXMLXLSMIPDTemplatePath = pstrIPDTemplatePath.Replace(".xls", ".xlsm");
Spire.Xls.Workbook lobjDSTemplateWorkbook = new Spire.Xls.Workbook();
lobjDSTemplateWorkbook.DisableMacrosStart = false;
lobjDSTemplateWorkbook.LoadFromFile(pstrIPDTemplatePath);
lobjDSTemplateWorkbook.SaveToFile(lstrOpenXMLXLSMIPDTemplatePath, Spire.Xls.ExcelVersion.Version2013);
pstrOpenXMLIPDTemplatePath = lstrOpenXMLXLSMIPDTemplatePath.Replace(".xlsm", ".xlsx");
using (var XLSMStream = File.OpenRead(lstrOpenXMLXLSMIPDTemplatePath))
using (var XLSXStream = new MemoryStream())
{
XLSMStream.CopyTo(XLSXStream);
using (var doc = SpreadsheetDocument.Open(XLSXStream, true))
{
doc.DeletePartsRecursivelyOfType<VbaDataPart>();
doc.DeletePartsRecursivelyOfType<VbaProjectPart>();
doc.ChangeDocumentType(DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook);
}
File.WriteAllBytes(pstrOpenXMLIPDTemplatePath, XLSXStream.ToArray());
}
Is there any to convert file directly to XLSX format with Spire.XLS Dll or some other way.
Yes, you can use Spire.XLS dll to convert XLS to XLSX format directly using the below code:
Workbook workbook = new Workbook();
workbook.LoadFromFile("input.xls");
//Set the version to Excel 2007 or above
workbook.SaveToFile("output.xlsx", ExcelVersion.Version2007);

NPOI produces unreadable content

I'm using NPOI to open an existing Excel file, make modifications, and write it back to disk.
However, when I open the file with NPOI and write it back, the file becomes damaged. Excel complains that the file "contains unreadable content", and asks whether I want to "recover the contents" of the file. When I select OK, it says:
Excel cannot open the file 'test.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Here is my code:
var excelFilename = "c:\\temp\\simplefile.xlsx";
IWorkbook wb;
using (var fs = File.OpenRead(excelFilename))
{
wb = new XSSFWorkbook(fs);
}
var sheet = wb.GetSheetAt(0);
// For this sample, we don't make any modifications to the file.
// Just opening and writing it back is enough to produce this error.
using (var str = new FileStream(excelFilename, FileMode.Open, FileAccess.ReadWrite))
{
wb.Write(str);
}
"simplefile.xlsx" is an empty excel workbook created by Excel 2010.
What's happening here?

Modifying an Excel File in Google Drive with Apache POI

I need to modify some cells of an Excel file stored in Google Drive. I'm using Apache POI to manipulate the Excel file and I can read and modify the file, but when I commit it to Google Drive it seems to work, it returns a success code but the file is not changed in Drive. The function I'm using to save the file is:
ParcelFileDescriptor file=result.getDriveContents().getParcelFileDescriptor();
InputStream in=new FileInputStream(file.getFileDescriptor());
HSSFWorkbook wb = new HSSFWorkbook(in);
for(Componente c:listaComponentes){
HSSFSheet sheet=wb.getSheetAt(c.getHoja());
HSSFRow fila=sheet.getRow(c.getFila());
fila.getCell(celdaSerie).setCellValue(c.getSerie());
}
FileOutputStream fileOut = new FileOutputStream(file.getFileDescriptor());
wb.write(fileOut);
result.getDriveContents().commit(mGoogleApiClient, null);

chart formatting lost when save as xlsx in NPOI

I have a template xlsx, called "book3.xlsx" that contains a pre-formatted chart. All I want to do is to read my template xlsx and modify some cells and write out the a xlsx ("test.xlsx")
A strange thing happens here, if i open the result xlsx the format of the chart has changed... It has different coloring, axis is missing etc. When I used xls file format it was fine, but we should use xlsx. Anyone noticed this issue?
var file = new FileStream(#"book3.xlsx", FileMode.Open, FileAccess.Read);
var workbook = new XSSFWorkbook(file);
using (var fs = new FileStream(#"test.xlsx", FileMode.Create))
{
workbook.Write(fs);
}

Resources