|
apache poi软件包可以用来操作office文档,并且可以支持2007以上版本的文档,例如excel的.xslx文件。下面是一个生成excel文件的例子,演示了常用的格式设置。
import java.awt.Color;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
public class Exemple1 {
public void run(String[] args) throws Throwable {
String[] titles = new String[] { "字符串", "单价", "数量", "日期", "百分比" };
String[][] datas = new String[][] {
{ "哇哈哈营养快线", "4.2", "2", "2014-06-04", "0.0343" },
{ "乐事薯片", "5.804", "1", "2014-06-04", "0.02335" } };
// 流形式的工作簿,适合数据量很大的情况
SXSSFWorkbook wb = new SXSSFWorkbook(100);
Sheet sh = wb.createSheet();
Row row;
Cell cell;
String[] rowDatas;
/* 写表头 */
XSSFCellStyle titleCellStyle = buildCellStyle(wb, false, null);
// 设置单元格背景色
titleCellStyle.setFillForegroundColor(new XSSFColor(new Color(247, 150,
90)));
// 设置纯色块填充
titleCellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
// 设置文本居中对齐
titleCellStyle.setAlignment(CellStyle.ALIGN_CENTER);
// 定义标题字体
Font titleFont = wb.createFont();
// 字体名称
titleFont.setFontName("微软雅黑");
// 字体为粗体
titleFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
titleCellStyle.setFont(titleFont);
// 创建标题行(第一行)
Row titleRow = sh.createRow(0);
// 创建标题行中每个标题
for (int cellnum = 0; cellnum < titles.length; cellnum++) {
cell = titleRow.createCell(cellnum);
cell.setCellStyle(titleCellStyle);
cell.setCellValue(titles[cellnum]);
}
/* 定义格式 */
Font dataFont = wb.createFont();
dataFont.setFontName("微软雅黑");
DataFormat format = wb.createDataFormat();
// 全边框,不换行,年月日格式的日期
CellStyle dateCellStyle = buildCellStyle(wb, false, format.getFormat("yyyy年m月d日"));
// 全边框,不换行,千位分隔符,两位小数
CellStyle double2CellStyle = buildCellStyle(wb, false, format.getFormat("#,##0.00"));
// 全边框,不换行,千位分隔符,三位小数
CellStyle double3CellStyle = buildCellStyle(wb, false, format.getFormat("#,##0.000"));
// 全边框,不换行,百分比,两位小数
CellStyle persentCellStyle = buildCellStyle(wb, false, format.getFormat("0.00%"));
// 全边框,不换行
CellStyle textCellStyle = buildCellStyle(wb, false, null);
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
/* 写数据 */
for (int rownum = 0; rownum < datas.length; rownum++) {
row = sh.createRow(rownum + 1);
rowDatas = datas[rownum];
cell = row.createCell(0);
cell.setCellStyle(textCellStyle);
cell.setCellValue(rowDatas[0]);
cell = row.createCell(1);
cell.setCellStyle(double2CellStyle);
cell.setCellValue(Double.valueOf(rowDatas[1]));
cell = row.createCell(2);
cell.setCellStyle(double3CellStyle);
cell.setCellValue(Double.valueOf(rowDatas[2]));
cell = row.createCell(3);
cell.setCellStyle(dateCellStyle);
cell.setCellValue(sf.parse(rowDatas[3]));
cell = row.createCell(4);
cell.setCellStyle(persentCellStyle);
cell.setCellValue(Double.valueOf(rowDatas[4]));
}
FileOutputStream out = new FileOutputStream("D:/temp/sxssf.xlsx");
wb.write(out);
out.close();
// dispose of temporary files backing this workbook on disk
wb.dispose();
System.out.println("程序运行结束。");
}
public static void main(String[] args) throws Throwable {
new Exemple1().run(args);
}
/**
* 构造单元格样式对象,已设置四周的边框。
* @param wb 工作薄对象
* @param isWrapText 单元格是否自动换行
* @param format 单元格格式化器编号
* @return 单元格样式对象
*/
private XSSFCellStyle buildCellStyle(SXSSFWorkbook wb, boolean isWrapText, Short format) {
XSSFCellStyle style = (XSSFCellStyle) wb.createCellStyle();
// 设置上边框为细线条
style.setBorderTop(CellStyle.BORDER_THIN);
// 设置下边框为细线条
style.setBorderBottom(CellStyle.BORDER_THIN);
// 设置左边框为细线条
style.setBorderLeft(CellStyle.BORDER_THIN);
// 设置右边框为细线条
style.setBorderRight(CellStyle.BORDER_THIN);
// 是否自动换行
style.setWrapText(isWrapText);
if (format != null) {
style.setDataFormat(format);
}
return style;
}
}
生成的excel文件如下图所示:
再看一个读取Excel文件的例子:
XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(new FileInputStream("D:/temp/test.xlsx"));
XSSFSheet sheet = wb.getSheetAt(0);
int lastRowNum = sheet.getLastRowNum();
// 从第2行(编号从0开始)读取数据,跳过标题行
for (int i = 1; i <= lastRowNum; i++) {
Row row = sheet.getRow(i);
String name = row.getCell(0).getStringCellValue();
Double price = row.getCell(1).getNumericCellValue();
Double count = row.getCell(2).getNumericCellValue();
Date date = row.getCell(3).getDateValue();
Double persent = row.getCell(4).getNumericCellValue();
} |
|
|