Recently, I met a question:
I need to export an excel(xlsx) with java, which must contain this kind of cell style:
I made a excel file with this vertical text, and exported as a xml file. Then I found that the style has an attribute named 'VerticalText':
By experience, I chose Apache POI. But I couldn't find any way to generate the cell style with POI. I could only find rotate method, which could't meet the requirement.
So I read more code of POI, and found that the cellstyles are build from some xsb file, which do not contain vertical text either.
Any help much appreciated.
The XML in your picture is Excel 2003 SpreadsheetML. But an *.xlsx file is a ZIP archive containing Office Open XML files. In that ZIP archive the styles.xml contains:
...
<cellXfs count="2">
...
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0">
<alignment textRotation="255"/>
</xf>
</cellXfs>
...
There <alignment textRotation="255"/> is for vertical text.
This can be set using apache poi like so:
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateXSSFVerticalText {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setRotation((short)255);
Sheet sheet = workbook.createSheet();
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("test");
cell.setCellStyle(cellStyle);
FileOutputStream fileOut = new FileOutputStream("CreateXSSFVerticalText.xlsx");
workbook.write(fileOut);
fileOut.close();
workbook.close();
}
}
Since the Office Open XML formats (like *.xlsx) are ZIP archives containing XML files it is pretty easy to determine the necessary XML attributes. Simply create a simple *.xlsx file having the needed formatting using the Excel GUI. Then unzip the *.xlsx file and have a look at xl/styles.xml.
Related
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);
I need to check the text format of a cell in .xlsx file (Microsoft Excel file) is strike through or not using Apache POI libraries.
Look following Image
Please Check this image !
I need to check whether B3 Cell text is strike through or not.
Finally I found a way to do this, code as follow
//Using workbook
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File("abc.xlsx")));
//Getting first sheet
XSSFSheet sheet = workbook.getSheetAt(0);
//Checking A1 cell that strikethrough or not
boolean strikeOutStatus=sheet.getRow(0).getCell(0).getCellStyle().getFont().getStrikeout();
System.out.println(strikeOutStatus);
Like this:
Get the XSSFRichTextString from the cell using XSSFCell#getRichStringCellValue()
Get the XSSFFont at a specific position using XSSFRichTextString#getFontAtIndex(int index)
Check the font using XSSFFont#getStrikeout()
Not sure how you get a reference to it but there's HSSFont.getStrikeout() and XSSFFont.getStrikeout()
Im using struts application. I want to work on reports. So going to try POI. I got this jar file poi-3.6-20091214.jar and put them in web-inf/lib folder.When i try using them in class file poi is not recognised. Im trying below...
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; //..
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet"); //Create a new row in current sheet
Row row = sheet.createRow(0); //Create a new cell in current row
Cell cell = row.createCell(0); //Set value to new value
cell.setCellValue("Blahblah");
But import itself wouldnt work.Any idea?
Have you added into the eclipse lib path?
another thing: use poi 3.8, is more recent. Why start with an old release?
I think its HSSFRow & HSSFCell and not Row and Cell. These are included in the jar.
One point to consider using POI is that the same code will not be able to handle both .xls and .xlsx file formats.
missed this: it will be XSSF if you are using .xlsx files and HSSF for .xls file format.
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).
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");
}
}