My ExcelPackage was working fine (xlsx) and I can read data, then I got new Excel file to import and all of sudden ExcelPackage stopped working.
After debug I found that excelPackage.Workbook.Worksheets is null and can't read worksheet.
One of my colleague asked me to SaveAs file and run code. I did accordingly and it's working now but it will not work for me because I used this code to download file from sftp server and there is hangfire routine which reads this file once a week, so it is auto routine where I can't save file before read.
public void ReadExcelFile(Stream stream, string worksheet)
{
var excelPackage = new ExcelPackage(stream);
var worksheet = excelPackage.Workbook.Worksheets[worksheet];
}
I expect from reader to read worksheet.
Related
I am new to Excel office scipt and not sure if there is a limitation running script on xls file vs xlsx file.
Below is my script which doesnt have an issue when I run on xlsx file.
However when I open a xls file it doesnt work and get error message "Line 6: Worksheet getUsedRange: There was an internal error while processing the request."
function main(workbook: ExcelScript.Workbook)
{
// Get the current worksheet
let ws = workbook.getActiveWorksheet();
//get active range of WorkSheet
let range = ws.getUsedRange();
// Create a table using the data range.
let newTable = ws.addTable(range, true);
newTable.setName("TableTKO");
}
I have been playing around and so far have observed the following:
when I save as the file from xls to xlsx it works
when I copy out the data from xls to a new spreadsheet (xlsx) it works
if I change the code and hardcode the range it will work
Any ideas why sometimes getting the used range work and is there an alternative of getting the last row/column with out using let range = ws.getUsedRange() as I dont want to hard code it?
Thanks
I tried with Aspose cell.
But I am able to add pdf file properly.
When I add jpg file, it shows in the excel file but doesnot get opened.
I tried with following way.
sheet.getOleObjects().get(oleObjectIndex).setImageData(binary);
sheet.getOleObjects().get(oleObjectIndex).setLeftCM(oleObjectIndex);
sheet.getOleObjects().get(oleObjectIndex).setDisplayAsIcon(true);
Here image shown like a thumbnail , but I dont want that.
sheet.getOleObjects().get(oleObjectIndex).setObjectData(binary);
//sheet.getOleObjects().get(oleObjectIndex).setFileType(oleFileType);
sheet.getOleObjects().get(oleObjectIndex).setDisplayAsIcon(true);
sheet.getOleObjects().get(oleObjectIndex).setLeftCM(oleObjectIndex);
Here it shows proper icon for the file but file does not get opened when double clicked.
Help from the community is highly apraciated.
Thank you.
See the following two lines of code that you also need to add:
sheet.getOleObjects().get(oleObjectIndex).setFileFormatType(FileFormatType.UNKNOWN);
sheet.getOleObjects().get(oleObjectIndex).setObjectSourceFullName(path);
Here is complete sample code that I tested and it works fine:
e.g
Sample code:
// Get the image file.
String path = "e:\\test\\myfile.jpg";
File file = new File(path);
// Get the picture into the streams.
byte[] img = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(img);
// Instantiate a new Workbook.
Workbook wb = new Workbook();
// Get the first worksheet.
Worksheet sheet = wb.getWorksheets().get(0);
// Add an Ole object into the worksheet with the image shown in MS Excel.
int oleObjIndex = sheet.getOleObjects().add(14, 3, 200, 220, img);
OleObject oleObj = sheet.getOleObjects().get(oleObjIndex);
// Set embedded ole object data and other attributes.
oleObj.setObjectData(img);
oleObj.setDisplayAsIcon(true);
oleObj.setFileFormatType(com.aspose.cells.FileFormatType.UNKNOWN);
oleObj.setObjectSourceFullName(path);
// Save the excel file
wb.save("f:\\files\\tstoleobject1.xlsx");
Hope, this helps a bit.
PS. I am working as Support developer/ Evangelist at Aspose.
i just use the same code,but i found when i open the new Excel,it shows as a image,i have to check it twice to transfer it to an OLE.
whats worry
I have implemented reading and converting to CSV from Excel File using Apache POI library successfully (where file is present in any of my local Windows folder) .
In my current scenario, I have to read the file from cloud storage bucket (gs://xyz/abc.xlsx),convert to CSV and store the file back to GCS.
Is there any way by which I can make my code identify the file present in Google Cloud?
Any leads will be appreciated.
There are Java APIs to communicate with GCS but I'm finding any way around to provide the path of GCS File to parameterized constructor of 'XSSFWorkbook'. Please find the snippet below which I'm using for converting an Excel file to CSV.
Workbook wb = new XSSFWorkbook(new File("C:\\Users\\balajeev\\Desktop\\abc.xlsx"));
DataFormatter formatter = new DataFormatter();
PrintStream out = new PrintStream(new FileOutputStream("C:\\Users\\balajeev\\Desktop\\abc.csv"),
true);
for (Sheet sheet : wb) {
for (Row row : sheet) {
boolean firstCell = true;
for (Cell cell : row) {
if ( ! firstCell ) out.print(',');
String text = formatter.formatCellValue(cell);
out.print(text);
firstCell = false;
}
out.println();
}
}
How to make this code recognise a file present in GCS,convert it to CSV and post conversion put the converted file in GCS.
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?
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);