原文:http://poi.apache.org/hssf/quick-guide.html
本文暂时给出一部分基础运用,其他内容待本人翻译完成之后再写出了。
创建一个新的Workbook
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
wb.write(fileOut);
fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
wb.write(fileOut);
fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// 创建一个行row同时在上面设置一些单元格,注意,行是从0开始,这里创建第一行。
HSSFRow row = sheet.createRow((short)0);
// 创建单元格并为他设置一个值,注意,单元格也是从0开始
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
// 可以在同一行创建多个单元格.
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);
// 将这些数据输出为Excel
FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
wb.write(fileOut);
fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
//创建一个行row同时在上面设置一些单元格,注意,行是从0开始,这里创建第一行
HSSFRow row = sheet.createRow((short)0);
// 创建一个单元格并为其设置时间值,第一个单元格是原始时间值Double类型,没有格式
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(new Date());
// 现在将第二个单元格格式化为日期+时间.
// 通过workbook创建一个新的单元格风格(cell style)是很重要的
// 否则你在修改这个单元格的风格的时候可能会影响到其他单元格的风格。
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell((short)1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
// 写出文件
FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
wb.write(fileOut);
fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)2);
row.createCell((short) 0).setCellValue(1.1);
row.createCell((short) 1).setCellValue(new Date());
row.createCell((short) 2).setCellValue("a string");
row.createCell((short) 3).setCellValue(true);
row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
// 写出文件
FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
wb.write(fileOut);
fileOut.close();
//一点废话:由此可见,在上面的关于日期的格式化我们不一定要通过poi来做,我们可以将数据都格式化好了以后,转换成String类型来写入Excel,这样从一定程度上统一写入单元格的代码,实现代码复用。
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 2);
createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
// 写入文件
FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
wb.write(fileOut);
fileOut.close();
}
/**
* 创建一个单元格使其按照某种方式排列
*
* @param wb the workbook
* @param row 生成单元格的行
* @param column 在这个行中单元格所处的列数
* @param align 单元格内容的排列方式.
*/
private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
{
HSSFCell cell = row.createCell(column);
cell.setCellValue("Align It");
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(align);
cell.setCellStyle(cellStyle);
}
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// 创建一行(row)并为其设置单元格,行从0开始.
HSSFRow row = sheet.createRow((short) 1);
// 创建单元格并为其设置数值.
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue(4);
// 在单元格周围设置边框.
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.GREEN.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLUE.index);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
style.setTopBorderColor(HSSFColor.BLACK.index);
cell.setCellStyle(style);
// 写入文件