Apache POI header error - excel

I am trying to read an xlsx file using Apache POI. Here is my code -
public static void convertFromXlsx(File inputFile, File outputFile) {
StringBuffer bf = new StringBuffer();
FileOutputStream fos = null;
String strGetValue = "";
try {
fos = new FileOutputStream(outputFile);
// XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(inputFile));
org.apache.poi.ss.usermodel.Workbook wb = WorkbookFactory.create(inputFile);
java.io.IOException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0
Please help me understand why this is happening and how it can be fixed.
There were intially 2 sheets in the xlsx file. I have tried with copies of the file - first removing the 2nd sheet and also by copying and pasting the content into a new sheet as text only but both lead to the same error.

Related

How to resolve XSSFWorkbook cannot be resolved to a type

Code outline of Test annotation:
void test() throws Exception{
String filePath = System.setProperty("user.dir", "https://d.docs.live.net/12e57c5f83beb2a2/Documents/Book1.xlsx");
FileInputStream fileStream = new FileInputStream(filePath);
XSSFWorkbook workbook = new XSSFWorkbook(fileStream);
XSSFSheet sheet = workbook.getSheet("Sheet1");
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
}
I'm trying to read the data from an xlsx file and that when I'm trying to create an Instance of the XSSFWorkbook, the compiler says
"- XSSFWorkbook cannot be resolved to a type"
Please help me how to solve this issue. It Would be really helpful.
Imported the below, will solve your issue.
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

How to fix "The supplied data appears to be in the Office 2007+ XML."

This is my code that extracts value from an xlsx file and print it on Eclipse console
public class testcode {
public void readexcel(String filepath, String filename, String sheetname) throws IOException
{
//Create an object of file class to open xlsx file
File file = new File(filepath+"\\"+filename);
//Create an object of FileInputStream to read an xlsx file
FileInputStream inputstream = new FileInputStream(file);
Workbook workbook = null;
//Find file extension name by using substring
String FileExtensionName = filename.substring(filename.indexOf("."));
//Check condition whether file is xlsx or xls
if(FileExtensionName.equalsIgnoreCase("xlsx"))
workbook = new XSSFWorkbook(inputstream);
else
workbook = new HSSFWorkbook(inputstream);
//Read sheet inside the workbook by its name
Sheet sheet = workbook.getSheet(sheetname);
//Find number of rows in sheet
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
//Create a loop over all the rows of excel file to read it
for(int i = 0; i<rowCount+1;i++)
{
Row row = sheet.getRow(i);
//Create loop to print cell values in a row
for(int j = 0; j<row.getLastCellNum();j++)
{
//Print excel value in console System.out.println(row.getCell(j).getStringCellValue()+"||");
}
System.out.println();
}
}
public static void main(String[] args) throws IOException {
testcode objExcelFile = new testcode();
//Prepare path of excel file
String filepath = "C:\\Users\\malfoy\\Desktop";
objExcelFile.readexcel(filepath,"testfile.xlsx", "read");
}
}
I am using office 2007 edition and I am getting an exception which says
"The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)"
How to fix it?
The line
String FileExtensionName = filename.substring(filename.indexOf("."));
returns a value with the dot (in your case ".xlsx")
So the following if statement returns a HSSFWorkbook instance instead of XSSFWorkbook.
To correct it use
String FileExtensionName = filename.substring(filename.lastIndexOf(".")+1);

How to create a new sheet and write some data in the existing excel using selenium java

I tried the below code.
But it overwrite the existing sheet.
File f= new File(System.getProperty("user.dir")+"\\src\\test\\resources\\Exceldata.xls");
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet= workbook.createSheet("Sheet4");
HSSFRow row = worksheet.createRow(1);
HSSFCell cell= row.createCell(1);
cell.setCellValue("admin");
enter code here
workbook.write(f);
workbook.close();
Use FileInputStream instead of File and and object of XSSFWorkbook
I hope this function may help you,
public static void write(){
try
{
FileInputStream myxls = new FileInputStream(System.getProperty("user.dir")+"\\src\\test\\resources\\Exceldata.xls" );
HSSFWorkbook studentsSheet = new HSSFWorkbook(myxls);
workbook = new XSSFWorkbook(myxls );
workbook.createSheet(sheetname);
HSSFSheet worksheet = studentsSheet.getSheetAt(0);
a=worksheet.getLastRowNum();
System.out.println(a);
Row row = worksheet.createRow(++a);
row.createCell(1).setCellValue("");
myxls.close();
FileOutputStream output_file =new FileOutputStream(new File(System.getProperty("user.dir")+"\\src\\test\\resources\\Exceldata.xls"));
//write changes
workbook.write(output_file );
studentsSheet.write(output_file);
output_file.close();
System.out.println(" is successfully written");
}
Try calling this function from main maethod,
public static void main(String args[])
{
write();
}
Possible duplicate of Append Data in existing Excel file using apache poi in java and
How to add new sheets to existing excel workbook using apache POI?
I finally found out the solution. i had used poi jar 4.0 in which i could not succeed in writing data. I then downgraded the jar version to 3.14 and i works perfectly

How to copy a worksheet from a workbook to new Workbook in Epplus?

try
{
FileInfo resultFile = new FileInfo(#"C:\\Users\\350154\\Desktop\\vb workouts\\testsample.xlsx");
if (resultFile.Exists)
{
resultFile.Delete();
}
ExcelPackage masterPackage = new ExcelPackage(new FileInfo(#"C:\\Users\\350154\\Desktop\\vb workouts\\testsample.xlsx"));
ExcelPackage pckg = new ExcelPackage(new FileInfo(#"C:\\Users\\350154\\Desktop\\vb workouts\\y.xlsx"));
string workSheetName = pckg.Workbook.Worksheets[1].Name;
masterPackage.Workbook.Worksheets.Add(workSheetName, pckg.Workbook.Worksheets[1]);
masterPackage.Workbook.Worksheets.Add("test");
masterPackage.SaveAs(new FileInfo(#"C:\\Users\\350154\\Desktop\\vb workouts\\testsample.xlsx"));
}catch(excption e){
console.writeline();
}
Hi,Am using Epplus to copy a worksheet from a workbook to a new workbook and create as a ne worksheet.My code is above.I am using Epplus4.0 .But am not able to process my code.Error is arising:Error Saving File
please help me to solve my issue.

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);

Resources