Một số khái niệm:
JExcelAPI, which is a mature, Java-based open source library that lets you read, write, and modify Excel spreadsheets. JExcelAPI's jexcelapi home directory contains a jxl.jar file that contains demos for reading, writing, and copying spreadsheets.jXLS 1.x provides jxls-reader module to read XLS files and populate Java beans with spreadsheet data.
Jxls v2.x tương tự jXLS 1.x nhưng đã có nhiều đặc tính mà phiên bản 1.x không có.
HSSF (Horrible SpreadSheet Format) – Use to read and write Microsoft Excel '97(-2007) (XLS) format files.
XSSF (XML SpreadSheet Format) – Used to reading and writting Excel 2007 OOXML - Open Office XML (.xlsx) format files.
SXSSF (package: org.apache.poi.xssf.streaming) is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limited. SXSSF achieves its low memory footprint by limiting access to the rows that are within a sliding window, while XSSF gives access to all rows in the document. Older rows that are no longer in the window become inaccessible, as they are written to the disk.
HWPF (Horrible Word Processor Format) – to read and write Microsoft Word 97 (DOC) format files.
HSMF (Horrible Stupid Mail Format) – pure Java implementation for Microsoft Outlook MSG files
HDGF (Horrible DiaGram Format) – One of the first pure Java implementation for Microsoft Visio binary files.
HPSF (Horrible Property Set Format) – For reading “Document Summary” information from Microsoft Office files.
HSLF (Horrible Slide Layout Format) – a pure Java implementation for Microsoft PowerPoint files.
HPBF (Horrible PuBlisher Format) – Apache's pure Java implementation for Microsoft Publisher files.
DDF (Dreadful Drawing Format) – Apache POI package for decoding the Microsoft Office Drawing format.
Opencsv is a very simple csv (comma-separated values) parser library for Java. It can dump out SQL tables to CSV:
java.sql.ResultSet myResultSet = .... writer.writeAll(myResultSet, includeHeaders);and It can bind CSV file to a list of Javabeans using CsvToBean, ColumnPositionMappingStrategy, HeaderColumnNameMappingStrategy classes.
WorkbookSettings wbSettings = new WorkbookSettings();
wbSettings.setEncoding("UTF-8");
Workbook workbookTemplate = null;
workbookTemplate = Workbook.getWorkbook(new File(templateRealPath), wbSettings);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
WritableWorkbook copy = Workbook.createWorkbook(outputStream, workbookTemplate);
workbookTemplate.close();
WritableSheet sheet = copy.getSheet(0);
sheet.setName(sheetName);
.....
copy.write(); // Writes out the data held in this workbook in Excel format, to outputStream
copy.close();
OutputStream os = new BufferedOutputStream(new FileOutputStream(realPath));
outputStream.writeTo(os);
outputStream.close();
Ví dụ dùng thư viện jxls 1.x:
package jxls.example;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import net.sf.jxls.transformer.XLSTransformer;
/**
*
* @author
*/
public class EmployeeEx {
public static void main(String[] args) throws Exception {
Collection staff = new HashSet();
staff.add(new Employee("Derek", 35, 3000, 0.30));
staff.add(new Employee("Elsa", 28, 1500, 0.15));
Map beans = new HashMap();
beans.put("employee", staff);
XLSTransformer transformer = new XLSTransformer();
transformer.transformXLS("employeeTemplate.xls", beans, "employeeOut.xls");
}
}
0 comments:
Post a Comment