I use Apache POI 3.13 to generate excel in cyrillic.
HSSFSheet sheet = workbook.createSheet("Лист");
I also tried:
HSSFSheet sheet = workbook.createSheet(
new String("Лист".getBytes(Charset.forName("UTF-8")))
);
But i get this error:
Use XSSFSheet instead of HSSHSheet like this.
XSSFSheet sheet = workbook.createSheet("Лист");
For this use poi-ooxml 3.9.
When saving the java file, there will be a alert message. Click Save as UTF-8.
Here is the image of alert message: screenshot
Related
I am using Apache POI 4.1 to generate Excel reports. I have a requirement to include logo image as Header event in Sheet. I could find the only below code, which adds text in Header. Is there any way to include image in the Header section of Sheet?
SXSSFWorkbook workBook = new SXSSFWorkbook(100);
SXSSFSheet sheet = workBook.createSheet("Sample Sheet");
Header header= sheet.getHeader();
header.setCenter("Header");
I'm generating an Excel file in Java with POI 3.2 (Latest version I can use for my client).
Here is my code. As you can see I'm using HSSF because I need to make a XLS file.
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Reporting");
sheet.setColumnWidth(250,250);
HSSFRow Row;
HSSFCell Cell;
//Content part (doesn't matter)
IWDResource resource = WDResourceFactory.createCachedResource(
wb.getBytes(),
"workbook.xls",
WDWebResourceType.XLS);
wdContext.currentContextElement().setXls(resource);
Now after I've downloaded the XLS file, I want to open it but the file seems to be corrupt.
Image on link: http://tinyurl.com/nop52sh
When I'm pressing 2 times on 'Don't send', de excell file opens in correct form.
Any idea why?
Don't call wb.getBytes(), it doesn't do what you want. From the getBytes() javadoc
Method getBytes - get the bytes of just the HSSF portions of the XLS file. Use this to construct a POI POIFSFileSystem yourself.
Instead, if you want the overall xls file as a byte array, do
ByteArrayOutStream baos = new ByteArrayOutStream();
wb.write(baos);
byte[] xlsBytes = baos.toByteArray();
Finally, Apache POI 3.2 is ancient, over 5 years old now! You really ought to upgrade, see the changelog for an idea of all the bugs fixed since then
I have this piece of code to read from an xls file:
fileName = "...."
WorkbookSettings settings = new WorkbookSettings();
settings.setEncoding("Cp1252");
System.out.println("BEFORE");
Workbook w = Workbook.getWorkbook(new File(fileName), settings);
Sheet sheet = w.getSheet(1);
System.out.println("AFTER");
This is what I get in the console:
BEFORE
Warning: Text Object on sheet "Detalle" not supported - omitting
jxl.common.AssertionFailed
at jxl.common.Assert.verify(Assert.java:37)
at jxl.read.biff.SheetReader.handleObjectRecord(SheetReader.java:1811)
at jxl.read.biff.SheetReader.read(SheetReader.java:1059)
at jxl.read.biff.SheetImpl.readSheet(SheetImpl.java:716)
at jxl.read.biff.WorkbookParser.getSheet(WorkbookParser.java:257)
at MapMovInfoResource.postService(MapMovInfoResource.java:77)
The problem comes when I try to open the second sheet in that file. When I use the first sheet (w.getSheet(0)), it works fine.
Any ideas on how to solve this?
enter image description here
Its seem you have some value in excel which are not text. Please check excel value like in image, and if and special quotes are available then remove that and try again.
I'm using workbook.removeSheetAt() to remove a certain named sheet from a workbook. However, the workbook contains a new sheet called "Sheet3" after writing it out to disk. What gives? I'm not creating this new sheet anywhere in my code. Note: I am using XSSFWorkbook and all the other XSSF stuff.
Some more info: Here is a snippet from workbook.xml inside the output xlsx. I am deleting sheet with index=2, ostensibly sheetId=3. The template workbook I am starting with only has four sheets.
<sheets>
<sheet r:id="rId1" sheetId="1" name="Sheet1"/>
<sheet r:id="rId2" sheetId="2" name="Params" state="hidden"/>
<sheet r:id="rId4" sheetId="4" name="Warnings" state="hidden"/>
<sheet r:id="rId3" sheetId="5" name="Sheet3"/>
</sheets>
Apache POI is using index based sheets (kinda like an array). If you have a sheet at a position that is greater than x, there must exist a sheet at position x even if it is blank. You could try moving the sheets where n > x to position n - 1 using setSheetOrder
Seems like there is a bug report similar to this: Bug Report
What about hiding the sheet: setSheetHidden
How can I make read only cell in Excel using Java ?
You may use Apache POI library to achieve this. Here is a simple code sample:
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Test");
Row row = sheet.createRow(0);
CellStyle style = wb.createCellStyle();
style.setLocked(true);
cell = row.createCell(0);
cell.setCellStyle(style);
// this is important as locking is pnly activated if sheet is protected
sheet.protectSheet("");
Maybe you can try jExcel. It is quite simple and easy to use.
Take a look at the Apache POI project, specifically the HSSF & XSSF subprojects, that provides a Java library to manipulate Excel documents.