Apache Project: POI-HSSF - Java API To Access Microsoft Excel Format Files (ZT)
Description:
Jakarta_POI use Java read and write Excel (97-2002) document to meet the needs of the majority.
Just because there is a project to use this tool, and spent time passing translated to a POI itself with the Guide. Some savings and amendments, the project hopes to use this entry to help some people.
There are several POI since the project: HSSF Excel to achieve literacy. HSSF Following is the Home
Http://jakarta.apache.org/poi/hssf/index.html
The following presentation is based on the following address translation:
Http://jakarta.apache.org/poi/hssf/quick-guide.html
The current version 1.51 is a very long time within a stable version, but HSSF not based on the Sample
1.51 written, so when the need to use the appropriate attention.
In fact, a couple of POI following several different read and write Word focus on the HDF are under development.
XML under the FOP (http://xml.apache.org/fop/index.html)
Pdf documents can be output, but also a good tool
Contents:
Create a workbook
Create a sheet
Create cells
Date created cells
Set cell format
Description:
The following may be required to use the following categories
Import org.apache.poi.hssf.usermodel.HSSFCell;
Import org.apache.poi.hssf.usermodel.HSSFCellStyle;
Import org.apache.poi.hssf.usermodel.HSSFDataFormat;
Import org.apache.poi.hssf.usermodel.HSSFFont;
Import org.apache.poi.hssf.usermodel.HSSFRow;
Import org.apache.poi.hssf.usermodel.HSSFSheet;
Import org.apache.poi.hssf.usermodel.HSSFWorkbook;
Import org.apache.poi.hssf.util.HSSFColor;
Create workbook
HSSFWorkbook wb = new HSSFWorkbook ();
/ / Default constructor to create workbook
FileOutputStream fileOut = new FileOutputStream ( "workbook.xls");
/ / Designated file name
Wb.write (fileOut);
/ / Output to a file
FileOut.close ();
Create a sheet
HSSFWorkbook wb = new HSSFWorkbook ();
HSSFSheet sheet1 = wb.createSheet ( "new sheet");
/ / Create sheet workbook
HSSFSheet sheet2 = wb.createSheet ( "second sheet");
/ / Workbook to create another sheet
FileOutputStream fileOut = new FileOutputStream ( "workbook.xls");
Wb.write (fileOut);
FileOut.close ();
Create cells
HSSFWorkbook wb = new HSSFWorkbook ();
HSSFSheet sheet = wb.createSheet ( "new sheet");
/ / Attention to the many ways the code parameters are short and not int so it is necessary to do a type conversion
Sheet.createRow HSSFRow row = ((short) 0);
/ / Create his sheet
HSSFCell cell row.createCell = ((short) 0);
/ / Create a cell line
Cell.setCellValue (1);
/ / Set the value of cells
/ / Value of the parameter type in how double, String, boolean,
Row.createCell ((short) 1). SetCellValue (1.2);
Row.createCell ((short) 2). SetCellValue ( "This is a string");
Row.createCell ((short) 3). SetCellValue (true);
/ / Write the output to a file
FileOutputStream fileOut = new FileOutputStream ( "workbook.xls");
Wb.write (fileOut);
FileOut.close ();
Date created cells
HSSFWorkbook wb = new HSSFWorkbook ();
HSSFSheet sheet = wb.createSheet ( "new sheet");
Sheet.createRow HSSFRow row = ((short) 0);
HSSFCell cell row.createCell = ((short) 0);
/ / Value for the date set
Cell.setCellValue (new Date ());
HSSFCellStyle cellStyle = wb.createCellStyle ();
/ / Designated date display format
CellStyle.setDataFormat (HSSFDataFormat.getFormat ( "m / d / yy h: mm"));
Row.createCell cell = ((short) 1);
Cell.setCellValue (new Date ());
/ / Date display format cells
Cell.setCellStyle (cellStyle);
FileOutputStream fileOut = new FileOutputStream ( "workbook.xls");
Wb.write (fileOut);
FileOut.close ();
Set cell format cell format settings there are many forms of cell alignment, as the font settings,
Cells such as the background color, form more because only give some examples. In the following examples
POI1.5 may change in the specific Show API.
……….
/ / Aqua background
HSSFCellStyle style = wb.createCellStyle ();
/ / Create a style
Style.setFillBackgroundColor (HSSFCellStyle.AQUA);
/ / Set the style of the background color filling
Style.setFillPattern (HSSFCellStyle.BIG_SPOTS);
/ / Fill the pattern of type.
/ / There are many patterns such as:
/ / HSSFCellStyle.BIG_SPOTS
/ / HSSFCellStyle.FINE_DOTS
/ / HSSFCellStyle.SPARSE_DOTS, etc.
Style.setAlignment (HSSFCellStyle.ALIGN_CENTER);
/ / Center alignment
Style.setFillBackgroundColor (HSSFColor.GREEN.index);
/ / Set a background color unit
Style.setFillForegroundColor (HSSFColor.RED.index);
/ / Set the color cell
HSSFCell cell row.createCell = ((short) 1);
Cell.setCellValue ( "X");
Cell.setCellStyle (style);
The use of poi hssf generate a excel document will have a main category Workbook (equivalent to an excel file) method
Workbook.write (OutputStream) can be written response.getOutputStream () If prior settings inside the contentType response to excel and download names can be downloaded from the annex excel
HSSFWorkbook book = _proxy.expertExcel (_formBean, _login);
If (book! = Null)
(
Response.setContentType ( "application / ms-excel");
Response.setHeader ( "Content-Disposition"
"Attachment; filename =" + new String ( "derived Excel.xls." GetBytes (), "iso-8859-1"));
Book.write (response.getOutputStream ());
)
ExpertExcel them from the database or simply access data created in other places can excel.








0 Comments to “Apache Project: POI-HSSF - Java API To Access Microsoft Excel Format Files (ZT)”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.