POI3.5_HSSF_和XSSF_Excel操作快速入门
  TEZNKK3IfmPf 2024年03月22日 115 0
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的读取和写出操作,具体功能可以在实际应用时使用更多的格式。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年03月22日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年04月26日   36   0   0 classjavapublic
  TEZNKK3IfmPf   2023年11月13日   31   0   0 class对象
  TEZNKK3IfmPf   2023年11月15日   45   0   0 classjava
  TEZNKK3IfmPf   2023年11月13日   23   0   0 classjava
  TEZNKK3IfmPf   2023年11月15日   26   0   0 classjavajvm
  TEZNKK3IfmPf   2024年03月22日   55   0   0 jar包class
  TEZNKK3IfmPf   2023年11月14日   33   0   0 classjava虚拟机
  TEZNKK3IfmPf   2023年11月13日   29   0   0 初始化classjava
TEZNKK3IfmPf