資源簡介
這個是POI3.1.7,目前官網上的最新版是4.0,但4.0中很多代碼是舊版POI不支持的,也許有人需要舊版的。
代碼片段和文件信息
package?com.mengdee.manager.util;
import?java.io.BufferedInputStream;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.io.OutputStream;
import?java.util.Date;
import?javax.swing.filechooser.FileSystemView;
import?org.apache.poi.hssf.usermodel.HSSFCell;
import?org.apache.poi.hssf.usermodel.HSSFCellstyle;
import?org.apache.poi.hssf.usermodel.HSSFCreationhelper;
import?org.apache.poi.hssf.usermodel.HSSFDataFormat;
import?org.apache.poi.hssf.usermodel.HSSFFont;
import?org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
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;
import?org.apache.poi.poifs.filesystem.POIFSFileSystem;
public?class?ExcelUtil?{
public?static?void?main(String[]?args)?throws?Exception?{
createExcel();
}
public?static?void?createExcel()?throws?IOException{
FileSystemView?fsv?=?FileSystemView.getFileSystemView();
String?desktop?=?fsv.getHomeDirectory().getPath();
String?filePath?=?desktop?+?“/template.xls“;
File?file?=?new?File(filePath);
OutputStream?outputStream?=?new?FileOutputStream(file);
HSSFWorkbook?workbook?=?new?HSSFWorkbook();
HSSFSheet?sheet?=?workbook.createSheet(“Sheet1“);
HSSFRow?row?=?sheet.createRow(0);
row.createCell(0).setCellValue(“id“);
row.createCell(1).setCellValue(“訂單號“);
row.createCell(2).setCellValue(“下單時間“);
row.createCell(3).setCellValue(“個數“);
row.createCell(4).setCellValue(“單價“);
row.createCell(5).setCellValue(“訂單金額“);
row.setHeightInPoints(30);?//?設置行的高度
HSSFRow?row1?=?sheet.createRow(1);
row1.createCell(0).setCellValue(“1“);
row1.createCell(1).setCellValue(“NO00001“);
//?日期格式化
HSSFCellstyle?cellstyle2?=?workbook.createCellstyle();
HSSFCreationhelper?creationhelper?=?workbook.getCreationhelper();
cellstyle2.setDataFormat(creationhelper.createDataFormat().getFormat(“yyyy-MM-dd?HH:mm:ss“));
sheet.setColumnWidth(2?20?*?256);?//?設置列的寬度
HSSFCell?cell2?=?row1.createCell(2);
cell2.setCellstyle(cellstyle2);
cell2.setCellValue(new?Date());
row1.createCell(3).setCellValue(2);
//?保留兩位小數
HSSFCellstyle?cellstyle3?=?workbook.createCellstyle();
cellstyle3.setDataFormat(HSSFDataFormat.getBuiltinFormat(“0.00“));
HSSFCell?cell4?=?row1.createCell(4);
cell4.setCellstyle(cellstyle3);
cell4.setCellValue(29.5);
//?貨幣格式化
HSSFCellstyle?cellstyle4?=?workbook.createCellstyle();
HSSFFont?font?=?workbook.createFont();
font.setFontName(“華文行楷“);
font.setFontHeightInPoints((short)15);
font.setColor(HSSFColor.RED.index);
cellstyle4.setFont(font);
HSSFCell?cell5?=?row1.createCell(5);
cell5.setCellFormula(“D2*E2“);??//?設置計算公式
//?獲取計算公式的值
HSSFFormulaEvaluator?e?=?new?HSSFFormulaEvaluator(workbook);
cell5
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????3688703??2018-08-31?16:47??xm
?????文件?????591748??2018-08-13?09:16??commons-compress-1.18.jar
?????文件???????4344??2018-08-14?16:36??ExcelUtil.java
?????文件???30044180??2018-08-14?10:37??poi-bin-3.17-20170915.zip
?????文件????????319??2018-08-31?11:11??poi下載地址.txt
-----------?---------??----------?-----??----
?????????????34329294????????????????????5
- 上一篇:阿里技術參考手冊研發篇
- 下一篇:ATOK安卓版日文輸入法V1.8.4 已付費版
評論
共有 條評論