poi生成Excel

1、无模板生成Excel的方法
Java代码 复制代码 收藏代码
  1. package com.test;
  2. import java.io.FileOutputStream;
  3. import java.util.Date;
  4. import org.apache.poi.hssf.usermodel.HSSFCell;
  5. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  6. import org.apache.poi.hssf.usermodel.HSSFDataFormat;
  7. import org.apache.poi.hssf.usermodel.HSSFFont;
  8. import org.apache.poi.hssf.usermodel.HSSFHyperlink;
  9. import org.apache.poi.hssf.usermodel.HSSFRow;
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  12. import org.apache.poi.hssf.util.HSSFColor;
  13. import org.apache.poi.ss.util.CellRangeAddress;
  14. public class PoiCreateExcel {
  15. public static void main(String[] args) throws Exception {
  16. // 创建Excel的工作书册 Workbook,对应到一个excel文档
  17. HSSFWorkbook wb = new HSSFWorkbook();
  18. // 创建Excel的工作sheet,对应到一个excel文档的tab
  19. HSSFSheet sheet = wb.createSheet("sheet1");
  20. // 设置excel每列宽度
  21. sheet.setColumnWidth(0, 4000);
  22. sheet.setColumnWidth(1, 3500);
  23. // 创建字体样式
  24. HSSFFont font = wb.createFont();
  25. font.setFontName("Verdana");
  26. font.setBoldweight((short) 100);
  27. font.setFontHeight((short) 300);
  28. font.setColor(HSSFColor.BLUE.index);
  29. // 创建单元格样式
  30. HSSFCellStyle style = wb.createCellStyle();
  31. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  32. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  33. style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
  34. style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  35. // 设置边框
  36. style.setBottomBorderColor(HSSFColor.RED.index);
  37. style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  38. style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  39. style.setBorderRight(HSSFCellStyle.BORDER_THIN);
  40. style.setBorderTop(HSSFCellStyle.BORDER_THIN);
  41. style.setFont(font);// 设置字体
  42. // 创建Excel的sheet的一行
  43. HSSFRow row = sheet.createRow(0);
  44. row.setHeight((short) 500);// 设定行的高度
  45. // 创建一个Excel的单元格
  46. HSSFCell cell = row.createCell(0);
  47. // 合并单元格(startRow,endRow,startColumn,endColumn)
  48. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));
  49. // 给Excel的单元格设置样式和赋值
  50. cell.setCellStyle(style);
  51. cell.setCellValue("hello world");
  52. // 设置单元格内容格式
  53. HSSFCellStyle style1 = wb.createCellStyle();
  54. style1.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));
  55. style1.setWrapText(true);// 自动换行
  56. row = sheet.createRow(1);
  57. // 设置单元格的样式格式
  58. cell = row.createCell(0);
  59. cell.setCellStyle(style1);
  60. cell.setCellValue(new Date());
  61. // 创建超链接
  62. HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
  63. link.setAddress("http://www.baidu.com");
  64. cell = row.createCell(1);
  65. cell.setCellValue("百度");
  66. cell.setHyperlink(link);// 设定单元格的链接
  67. FileOutputStream os = new FileOutputStream("D:\\report\\workbook.xls");
  68. wb.write(os);
  69. os.close();
  70. }
  71. }
package com.test;

import java.io.FileOutputStream;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
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.ss.util.CellRangeAddress;

public class PoiCreateExcel {

public static void main(String[] args) throws Exception {
    // 创建Excel的工作书册 Workbook,对应到一个excel文档
    HSSFWorkbook wb = new HSSFWorkbook();

    // 创建Excel的工作sheet,对应到一个excel文档的tab
    HSSFSheet sheet = wb.createSheet("sheet1");

    // 设置excel每列宽度
    sheet.setColumnWidth(0, 4000);
    sheet.setColumnWidth(1, 3500);

    // 创建字体样式
    HSSFFont font = wb.createFont();
    font.setFontName("Verdana");
    font.setBoldweight((short) 100);
    font.setFontHeight((short) 300);
    font.setColor(HSSFColor.BLUE.index);

    // 创建单元格样式
    HSSFCellStyle style = wb.createCellStyle();
    style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
    style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
    style.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);
    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

    // 设置边框
    style.setBottomBorderColor(HSSFColor.RED.index);
    style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    style.setBorderRight(HSSFCellStyle.BORDER_THIN);
    style.setBorderTop(HSSFCellStyle.BORDER_THIN);

    style.setFont(font);// 设置字体

    // 创建Excel的sheet的一行
    HSSFRow row = sheet.createRow(0);
    row.setHeight((short) 500);// 设定行的高度
    // 创建一个Excel的单元格
    HSSFCell cell = row.createCell(0);

    // 合并单元格(startRow,endRow,startColumn,endColumn)
    sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));

    // 给Excel的单元格设置样式和赋值
    cell.setCellStyle(style);
    cell.setCellValue("hello world");

    // 设置单元格内容格式
    HSSFCellStyle style1 = wb.createCellStyle();
    style1.setDataFormat(HSSFDataFormat.getBuiltinFormat("h:mm:ss"));

    style1.setWrapText(true);// 自动换行

    row = sheet.createRow(1);

    // 设置单元格的样式格式

    cell = row.createCell(0);
    cell.setCellStyle(style1);
    cell.setCellValue(new Date());

    // 创建超链接
    HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
    link.setAddress("http://www.baidu.com");
    cell = row.createCell(1);
    cell.setCellValue("百度");
    cell.setHyperlink(link);// 设定单元格的链接

    FileOutputStream os = new FileOutputStream("D:\\report\\workbook.xls");
    wb.write(os);
    os.close();
}

}




注:HSSFWorkbook,XSSFWorkbook的区别:前者是解析出来excel 2007 以前版本的,后缀名为xls的,后者是解析excel 2007 版的,后缀名为xlsx。

文章来源:http://hi.baidu.com/suny%5Fduan/blog/item/d528d5112b03cff0c3ce79a6.html
2、有模板,文件头标题都写好,只要循环添加数据的方法
Java代码 复制代码 收藏代码
  1. package com.test;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  8. import org.apache.poi.ss.usermodel.Cell;
  9. import org.apache.poi.ss.usermodel.CellStyle;
  10. import org.apache.poi.ss.usermodel.Row;
  11. import org.apache.poi.ss.usermodel.Sheet;
  12. import org.apache.poi.ss.usermodel.Workbook;
  13. import org.apache.poi.ss.util.CellRangeAddress;
  14. public class PoiTestExcel {
  15. /**
  16. * @param args
  17. */
  18. public static void main(String[] args) {
  19. // TODO Auto-generated method stub
  20. try {
  21. InputStream in = new FileInputStream("D:\\report\\1111.xls");
  22. Workbook work = new HSSFWorkbook(in);
  23. // 得到excel的第0张表
  24. Sheet sheet = work.getSheetAt(0);
  25. // 得到第1行的第一个单元格的样式
  26. Row rowCellStyle = sheet.getRow(1);
  27. CellStyle columnOne = rowCellStyle.getCell(0).getCellStyle();
  28. // 这里面的行和列的数法与计算机里的一样,从0开始是第一
  29. // 填充title数据
  30. Row row = sheet.getRow(0);
  31. Cell cell = row.getCell(0);
  32. cell.setCellValue("2010年花名测");
  33. int i = 2;//计数器
  34. int number = 0;
  35. // 得到行,并填充数据和表格样式
  36. for (;i < 10; i++) {
  37. row = sheet.createRow(i);// 得到行
  38. cell = row.createCell(0);// 得到第0个单元格
  39. cell.setCellValue("琳"+i);// 填充值
  40. cell.setCellStyle(columnOne);// 填充样式
  41. cell = row.createCell(1);
  42. cell.setCellValue("女");
  43. cell.setCellStyle(columnOne);// 填充样式
  44. cell = row.createCell(2);
  45. cell.setCellValue(i+20);
  46. cell.setCellStyle(columnOne);// 填充样式
  47. // .....给每个单元格填充数据和样式
  48. number++;
  49. }
  50. //创建每个单元格,添加样式,最后合并
  51. row = sheet.createRow(i);
  52. cell = row.createCell(0);
  53. cell.setCellValue("总计:" + number + "个学生");// 填充值
  54. cell.setCellStyle(columnOne);
  55. cell = row.createCell(1);
  56. cell.setCellStyle(columnOne);
  57. cell = row.createCell(2);
  58. cell.setCellStyle(columnOne);
  59. // 合并单元格
  60. sheet.addMergedRegion(new CellRangeAddress(i,i,0,2));
  61. FileOutputStream os = new FileOutputStream("D:\\report\\workbook.xls");
  62. work.write(os);
  63. os.close();
  64. } catch (FileNotFoundException e) {
  65. System.out.println("文件路径错误");
  66. e.printStackTrace();
  67. } catch (IOException e) {
  68. System.out.println("文件输入流错误");
  69. e.printStackTrace();
  70. }
  71. }
  72. }
### 使用 Apache POI 生成 Excel 文件 为了使用 Apache POI 库创建并保存 Excel 文件,在项目中需引入必要的 Maven 依赖: ```xml <dependencies> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> </dependencies> ``` 下面是一个简单的例子,展示如何利用上述配置好的环境编写 Java 程序来构建一个新的 Excel 工作簿,并向其中添加一些基础的数据。 #### 创建工作簿和表单 ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileOutputStream; import java.io.IOException; public class CreateExcel { public static void main(String[] args) throws IOException { // 创建新的工作簿实例 try (Workbook workbook = new XSSFWorkbook()) { // 添加新表格到工作簿内 Sheet sheet = workbook.createSheet("Example"); // 定义要写入的内容 Object[][] bookData = { {"书名", "作者", "出版年"}, {"Java编程思想", "Bruce Eckel", 2007}, {"深入理解计算机系统", "Randal E.Bryant, David R.O'Hallaron", 2018} }; int rowNum = 0; System.out.println("正在创建Excel文件..."); for (Object[] aBook : bookData) { Row row = sheet.createRow(rowNum++); int colNum = 0; for (Object field : aBook) { Cell cell = row.createCell(colNum++); if (field instanceof String) { cell.setCellValue((String) field); } else if (field instanceof Integer) { cell.setCellValue((Integer) field); } } } // 自动调整列宽以适应内容长度 for(int i = 0; i < 3; i++){ sheet.autoSizeColumn(i); } // 将工作簿写出至指定路径下的文件 try (FileOutputStream outputStream = new FileOutputStream("example.xlsx")) { workbook.write(outputStream); System.out.println("已成功创建Excel文件!"); } } } } ``` 这段代码展示了怎样通过 `XSSFWorkbook` 类创建一个 `.xlsx` 格式的 Excel 文档[^3]。此文档包含了名为 “Example” 的单一表格页签以及两行书籍信息记录。每条记录由三个字段组成——书名、作者姓名及其发布日期。最后一步是调用 `write()` 方法把整个工作簿序列化成字节流形式存储于磁盘上的特定位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值