public static void main(String[] args) throws Exception {
//创建excel
Workbook wb = new HSSFWorkbook();
//创建sheet
Sheet sheet = wb.createSheet("iloveyou");
//创建行
Row row = sheet.createRow(0);
//创建单元格
Cell cell = row.createCell(0);
//填充元素
cell.setCellValue("I Love You!");
//输出流
FileOutputStream out = new FileOutputStream("iloveyou.xls");
//写出
wb.write(out);
//关闭流
out.close();
}
public class PoiTest1 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建excel文件
Workbook wb = new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("dateSheet");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue(new Date());
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
//使用java.util.Calendar来设置单元格格式
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
}
public class PoiTest7 {
public static void main(String[] args) throws Exception {
Workbook wb = new XSSFWorkbook();
Sheet sheet = wb.createSheet("new sheet");
Row row = sheet.createRow((short)1);
CellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.YELLOW.getIndex());
style.setFillPattern(CellStyle.BIG_SPOTS);
Cell cell = row.createCell((short)1);
cell.setCellValue("X");
cell.setCellStyle(style);
style = wb.createCellStyle();
style.setFillBackgroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
cell = row.createCell((short)2);
cell.setCellValue("X");
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("style2.xlsx");
wb.write(fileOut);
fileOut.close();
}
}
通过4天学习,基本了解了POI的读取和写出操作,具体功能可以在实际应用时使用更多的格式。