这个是POI3.1.7,目前官网上的最新版是4.0,但4.0中很多代码是旧版POI不支持的,也许有人需要旧版的。
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3688703 2018-08-31 16:47 xmlbeans-bin-3.0.1-20180824.zip
文件 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
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
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)