- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Calendar;
- 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.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- public class ReadXlsTest {
- // 设置cell编码解决中文高位字节截断
- private static short XLS_ENCODING = HSSFWorkbook.ENCODING_UTF_16;
- // 定制日期格式
- private static String DATE_FORMAT = " m/d/yy "; // "m/d/yy h:mm"
- // 定制浮点数格式
- private static String NUMBER_FORMAT = " #,##0.00 ";
- private String xlsFileName;
- private HSSFWorkbook workbook;
- private HSSFSheet sheet;
- private HSSFRow row;
- /**
- * 初始化Excel
- *
- * @param fileName
- * 导出文件名
- */
- public ReadXlsTest(String fileName) {
- this.xlsFileName = fileName;
- this.workbook = new HSSFWorkbook();
- this.sheet = workbook.createSheet();
- }
- /**
- * 导出Excel文件
- *
- * @throws XLSException
- */
- public void exportXLS() throws Exception {
- try {
- FileOutputStream fOut = new FileOutputStream(xlsFileName);
- workbook.write(fOut);
- fOut.flush();
- fOut.close();
- } catch (FileNotFoundException e) {
- throw new Exception(" 生成导出Excel文件出错! ", e);
- } catch (IOException e) {
- throw new Exception(" 写入Excel文件出错! ", e);
- }
- }
- /**
- * 增加一行
- *
- * @param index
- * 行号
- */
- public void createRow(int index) {
- this.row = this.sheet.createRow(index);
- }
- /**
- * 设置单元格
- *
- * @param index
- * 列号
- * @param value
- * 单元格填充值
- */
- public void setCell(int index, String value) {
- HSSFCell cell = this.row.createCell((short) index);
- cell.setCellType(HSSFCell.CELL_TYPE_STRING);
- cell.setEncoding(XLS_ENCODING);
- cell.setCellValue(value);
- }
- /**
- * 设置单元格
- *
- * @param index
- * 列号
- * @param value
- * 单元格填充值
- */
- public void setCell(int index, Calendar value) {
- HSSFCell cell = this.row.createCell((short) index);
- cell.setEncoding(XLS_ENCODING);
- cell.setCellValue(value.getTime());
- HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
- cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(DATE_FORMAT)); // 设置cell样式为定制的日期格式
- cell.setCellStyle(cellStyle); // 设置该cell日期的显示格式
- }
- /**
- * 设置单元格
- *
- * @param index
- * 列号
- * @param value
- * 单元格填充值
- */
- public void setCell(int index, int value) {
- HSSFCell cell = this.row.createCell((short) index);
- cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
- cell.setCellValue(value);
- }
- /**
- * 设置单元格
- *
- * @param index
- * 列号
- * @param value
- * 单元格填充值
- */
- public void setCell(int index, double value) {
- HSSFCell cell = this.row.createCell((short) index);
- cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
- cell.setCellValue(value);
- HSSFCellStyle cellStyle = workbook.createCellStyle(); // 建立新的cell样式
- HSSFDataFormat format = workbook.createDataFormat();
- cellStyle.setDataFormat(format.getFormat(NUMBER_FORMAT)); // 设置cell样式为定制的浮点数格式
- cell.setCellStyle(cellStyle); // 设置该cell浮点数的显示格式
- }
- public static void main(String[] args) {
- System.out.println( " 开始导出Excel文件 " );
- ReadXlsTest e = new ReadXlsTest("C://test.xls");
- e.createRow( 0 );
- e.setCell( 0 , " 编号 " );
- e.setCell( 1 , " 名称 " );
- e.setCell( 2 , " 日期 " );
- e.setCell( 3 , " 金额 " );
- e.createRow( 1 );
- e.setCell( 0 , 1 );
- e.setCell( 1 , " 工商银行 " );
- e.setCell( 2 , Calendar.getInstance());
- e.setCell( 3 , 111123.99 );
- e.createRow( 2 );
- e.setCell( 0 , 2 );
- e.setCell( 1 , " 招商银行 " );
- e.setCell( 2 , Calendar.getInstance());
- e.setCell( 3 , 222456.88 );
- try {
- e.exportXLS();
- System.out.println( " 导出Excel文件[成功] " );
- } catch (Exception e1) {
- System.out.println( " 导出Excel文件[失败] " );
- e1.printStackTrace();
- }
- }
- }
- Excel文档的模板可以从文件系统中引入,再将数据插入即可:
- HSSFWorkbook wb = null;
- InputStream is = null;
- try
- {
- is = getClass().getResourceAsStream("/com/util/abc.xls");
- POIFSFileSystem fs = new POIFSFileSystem(is);
- wb = new HSSFWorkbook(fs);
- }catch(Exception e){
- e.printStackTrace();
- }finally{
- try{
- if(null!=is)
- is.close();
- }catch(Exception e){
- e.printStackTrace();
- }
- }
poi导出Excel
最新推荐文章于 2024-08-17 10:05:48 发布