Invalid Header Signature - java

Here I want to extract the data from .xlsx file and for that I already add the poi jar and created the reference of fileInputStream
package demo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class DemoExcel {
public static void main(String[] args) throws Exception{
File excel = new File("C:\\Users\\Devaditya\\Documents\\Book1.xlsx");
FileInputStream fis = null;
fis = new FileInputStream(excel);
System.out.println(fis.toString());
HSSFWorkbook wb = new HSSFWorkbook(fis);
System.out.println(wb.toString());
HSSFSheet sh = wb.getSheet("Data");
System.out.println(sh.toString());
}
}
Here i am getting the error:-
Exception in thread "main" java.io.IOException: Invalid header signature; read 0, expected -2226271756974174256
at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:88)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:83)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:210)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:191)
at demo.DemoExcel.main(DemoExcel.java:23)

Lets start with types of WorkBook.
HSSFWorkbook
This is old binary proprietary Excel format, know by the extension .xls.
XSSFWorkbook
This is the new XML Excel format, known by the extension .xlsx.
So, you are using the wrong class.
In fact it would be better not to use a specific class at all, let POI work out what you have. Use a WorkbookFactory:
final Workbook workbook = WorkbookFactory.create(excel);
This is:
programming to the interface.
robust to changes in the type of workbook read, as long a POI supports it
faster and more efficient. POI can read the File piecemeal, when it needs to rather than having to slurp the whole workbook into memory.
Doesn't have the memory leak that you have when you don't close() the FileInputStream.

try to open the file in microsoft office Excel then a pop message will appear to inform you that that file you are trying to open isn't an excel file, and give you the choice to open it any way, so just open it and save it as excel file.

Related

How to set Page Breaks View Mode for .xls using hssfwork book ApachePOI

I am creating an .xls excel file using apache poi. I need to set the Page Breaks View by default. But I did look at a related question on .xlsx file . I didn't find anything "how to set Page Breaks View Mode" to using HSSF of ApachePOI
enter image description here
The binary BIFF file system of *.xls and the Office Open XML file system of *.xlsx are two totally different file systems. You can't mix them. In apache poi HSSF ist for the one and XSSF for the other. The high level classes of apache poi try providing methods for both the file systems. This gets done using interfaces in SS. But outside the high level classes one needs strictly differentiate the both file systems.
Setting page break preview for a sheet is not provided by the high level classes up to now. So we need the underlyinf low lewel classes. For XSSF this are the org.openxmlformats.schemas.spreadsheetml.x2006.main.* classes, which are the XML representation of the XSSF internals. But for HSSF this are the org.apache.poi.hssf.record.* classes, which are the binary representation of the HSSF internals.
Setting page break preview for a sheet could be done like so for both the file systems:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.model.InternalSheet;
import org.apache.poi.hssf.record.WindowTwoRecord;
public class ExcelPageBreakPreview {
public static void main(String[] args) throws Exception {
//Workbook workbook = WorkbookFactory.create(new FileInputStream("./ExcelTemplate.xlsx")); String filePath = "./ExcelInPageBreakPreview.xlsx";
Workbook workbook = WorkbookFactory.create(new FileInputStream("./ExcelTemplate.xls")); String filePath = "./ExcelInPageBreakPreview.xls";
Sheet sheet = workbook.getSheetAt(0);
//set sheet in PageBreakPreview
if (sheet instanceof XSSFSheet) {
XSSFSheet xssfSheet= (XSSFSheet)sheet;
xssfSheet.lockSelectLockedCells(true);
xssfSheet.getCTWorksheet().getSheetViews().getSheetViewArray(0).setView(org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetViewType.PAGE_BREAK_PREVIEW);
} else if (sheet instanceof HSSFSheet) {
HSSFSheet hssfSheet= (HSSFSheet)sheet;
InternalSheet internalSheet = hssfSheet.getSheet();
WindowTwoRecord record = internalSheet.getWindowTwo();
record.setSavedInPageBreakPreview(true);
}
FileOutputStream fileOut = new FileOutputStream(filePath);
workbook.write(fileOut);
fileOut.close();
workbook.close();
}
}
Previous apache poi versions might not have InternalSheet HSSFSheet.getSheet public. Then one needs using reflection to get the InternalSheet:
//InternalSheet internalSheet = hssfSheet.getSheet();
java.lang.reflect.Field _sheet = HSSFSheet.class.getDeclaredField("_sheet");
_sheet.setAccessible(true);
InternalSheet internalSheet = (InternalSheet)_sheet.get(hssfSheet);

Unable to read excel files in java getting 2 below error in java

I am trying to read xls and xlsx files with below code and get data from it but i am able to read 70% of xl files i am having but for some i am getting below errors
for remaining files some are giving 1st error and some are giving 2nd error
1)java.lang.IllegalArgumentException: The supplied POIFSFileSystem does not contain a BIFF8 'Workbook' entry. Is it really an excel file?
2)java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
Both above errors are coming for xlsx extension files
Why am i able to read some and not some files and is there any global solution to this
My java code:
FileInputStream input = new FileInputStream("/Users/Xl_Files/"+fileName);
Workbook wb = WorkbookFactory.create(input);
Sheet sheet = wb.getSheetAt(0);
String var = String.valueOf(sheet.getRow(Row).getCell(Colon));

Files vs InputStreams

I'm reading a excel file by Poi(3.7). I'm learning somethins about poi at this link Poi Quick Guide. Now my concerns is about this:
When opening a workbook, either a .xls HSSFWorkbook, or a .xlsx XSSFWorkbook, the Workbook can be loaded from either a File or an InputStream. Using a File object allows for lower memory consumption, while an InputStream requires more memory as it has to buffer the whole file
In the 3.7 version to Poi the WorkbookFactory doesn't have following method
WorkbookFactory.create(new File("MyExcel.xls"))
and i try to load my file in these ways:
First way
InputStream is = (InputStream) getClass().getResourceAsStream("/MyExcel.xlsx");
Workbook wb = WorkbookFactory.create(is);
Second way
String path = getClass().getResource("/MyExcel.xlsx").getPath();
FileInputStream fis = new FileInputStream(new File(path));
Workbook wb = WorkbookFactory.create(fis);
Now i want to ask you, what is background difference of these three possibilities to load an excel file? Which of these do you suggest?
First way is good if your file is located in file system. Second way is actually wrong. If your file is a part of your application, i.e. avaliable from the classpath use the following code:
Workbook wb = WorkbookFactory.create(getClass().getResourceAsStream("/MyExcel.xlsx").getPath());
What's wrong with your code? Actually it may work only if your classes are located directly in file system. If however they are packed into jar the line new FileInputStream(new File(path)) will throw FileNotFoundException because file indeed does not exist in file system but packed into jar.
Using an InputStream has a higher memory footprint than using a File

Column limit in jxlapi for producing excel files from Java

I want to produce an excel file using Java and jxlapi. But it is not allowing me to use more than 256 columns. I want to increase the column limit to at least 1000.
I tried ApachePOI also but the same problem.It is not allowing me more than 256 columns.
Any insight on how to achieve this?
Here is my simple implementation of Apache POI which sends me into a never ending loop of importing libraries.. :(
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
public class CreateExcel {
public static void main (String args[]) throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("new sheet");
XSSFRow row1 = sheet.createRow((short)0);
XSSFRow row2 = sheet.createRow((short)0);
XSSFCell tempcell1 = row1.createCell(3);
tempcell1.setCellValue(1000);
FileOutputStream fileOut = new FileOutputStream("/home/abhishek/Desktop/workbook1.xls");
wb.write(fileOut);
fileOut.close();
}
}
JExcel (jxlapi) writes Excel files in the Excel 2000 file format (.XLS extension). This format is limited to 256 columns.
You might need to change to another library able to write Excel files in the Excel 2003 file format (.XLSX extension), which doesn't have this limitation anymore.
Apache POI is able to write the newer file format if you use the classes in the org.apache.poi.ss.usermodel packages (instead of the org.apache.poi.hssf.usermodel package).

Handling Excel file in Java

I have a Excel file that I have to read the file and go line by line and check the first column. Here is an example of the column headers
ISBN#13 Run Date Title Author Type
So I have to check each ISBN#13 and determine if it is an isbn#13, format it and write the whole line to a file. Then take all the ones that are not ISBN#13 and write them to a file.
So the question is how do I check the column "ISBN#13" and how do I write each row to a file. It would be another excel file.
There is also xslx4j (part of docx4j) if you are working with xlsx only (ie not the legacy binary .xls) and prefer to use jaxb
The Apache poi (Poor Obfuscation Implementation) project is made for reading from and writing to Excel files in Java:
http://poi.apache.org/
Code example:
import java.io.*;
import org.apache.poi.xssf.usermodel.*;
public class CreateWorkBook
{
public static void main(String[] args)throws Exception
{
//Create Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
//Create file system using specific name
FileOutputStream out = new FileOutputStream(
new File("createworkbook.xlsx"));
//write operation workbook using file out object
workbook.write(out);
out.close();
System.out.println("createworkbook.xlsx written successfully");
}
}

Categories