生成excel文件

java操作Excel最常用的开源组件有poi与jxl。jxl是韩国人开发的,发行较早,但是更新的很慢,目前似乎还不支持excel2007。poi是apache下的一个子项目,poi应该是处理ms的office系列文档最好的组件了。

poi的jar包下载地址:

http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.10-FINAL-20140208.zip

  1 /**
  2      * @ excel
  3      */
  4     public static void excelDemo() {
  5         // 创建一个工作薄,返回这个工作薄的引用
  6         HSSFWorkbook workbook = new HSSFWorkbook();
  7         // 新建一个工作表,返回这个表的引用
  8         HSSFSheet sheet = workbook.createSheet("sheetOne");
  9         // 创建一行,作为head。下标从0开始,返回行的引用
 10         HSSFRow row = sheet.createRow(0);
 11         // 创建单元格样式对象
 12         HSSFCellStyle headStyle = workbook.createCellStyle();
 13         // 水平方向居中
 14         headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 15         // 垂直方向居中
 16         headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
 17         // 创建字体属性
 18         HSSFFont font1 = workbook.createFont();
 19         // 字体颜色
 20         font1.setColor(HSSFFont.COLOR_RED);
 21         // 粗体
 22         font1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
 23         // 字体大小
 24         font1.setFontHeightInPoints((short) 14);
 25         // 将字体对象放到样式对象中
 26         headStyle.setFont(font1);
 27         // 单元格对象
 28         HSSFCell  cell;
 29         // 在该行创建6个单元格
 30         for (int i = 0; i < 5; i++) {
 31             cell = row.createCell(i);
 32             // 设定单元格为字符类型
 33             cell.setCellType(HSSFCell.CELL_TYPE_STRING);
 34             // 设定单元格样式 
 35             cell.setCellStyle(headStyle);
 36             // 设定单元格内容
 37             cell.setCellValue("<head>"+i);
 38         }
 39         
 40         // body的样式
 41         HSSFCellStyle bodyStyle = workbook.createCellStyle();
 42         // 横向中间对齐
 43         bodyStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 44         // 纵向中间对齐
 45         bodyStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
 46         // body的字体属性设定
 47         HSSFFont font2 = workbook.createFont();
 48         // 加粗
 49         font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
 50         // 字体颜色
 51         font2.setColor(IndexedColors.BLUE.getIndex());
 52         bodyStyle.setFont(font2);
 53         
 54         // 创建3行,作为body
 55         for (int i = 1; i < 4; i++) {
 56             row = sheet.createRow(i);
 57             for (int j = 0; j < 6; j++) {
 58                 cell = row.createCell(j);
 59                 cell.setCellStyle(bodyStyle);
 60                 cell.setCellType(HSSFCell.CELL_TYPE_STRING);
 61                 cell.setCellValue("<body>");
 62             }
 63         }
 64         
 65         // 合并单元格
 66         // 首先把单元格创建出来
 67         for (int i = 4; i < 7; i++) {
 68             row = sheet.createRow(i);
 69             for (int j = 0; j < 6; j++) {
 70                 cell = row.createCell(j);
 71                 // 设定单元格为字符类型
 72                 cell.setCellType(HSSFCell.CELL_TYPE_STRING);
 73             }
 74         }
 75         
 76         // 合并第5行的0到6列
 77         sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, 5));
 78         // 合并第6行到第7行的1到3列
 79         sheet.addMergedRegion(new CellRangeAddress(5, 6, 0, 2));
 80         // 合并第6行到第7行的3到6列
 81         sheet.addMergedRegion(new CellRangeAddress(5, 6, 3, 5));
 82         
 83         // 获取第6行第1列的单元格
 84         row = sheet.getRow(5);
 85         cell = row.getCell(0);
 86         // 字体属性的设定
 87         HSSFFont hebingFont = workbook.createFont();
 88         // 字体颜色
 89         hebingFont.setColor(IndexedColors.BRIGHT_GREEN.getIndex());
 90         // 单元格样式设定
 91         HSSFCellStyle hebingStyle1 = workbook.createCellStyle();
 92         // 横向中间对齐
 93         hebingStyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
 94         // 纵向中间对齐
 95         hebingStyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
 96         // 设定前景色
 97         hebingStyle1.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
 98         hebingStyle1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
 99         // 设定字体
100         hebingStyle1.setFont(hebingFont);
101         // 设定单元格样式
102         cell.setCellStyle(hebingStyle1);
103         // 设定单元格内容
104         cell.setCellValue("合并单元格");
105         
106         cell = row.getCell(3);
107         HSSFCellStyle hebingStyle2 = workbook.createCellStyle();
108         hebingStyle2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
109         hebingStyle2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
110         hebingStyle2.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
111         hebingStyle2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
112         hebingStyle2.setFont(hebingFont);
113         cell.setCellStyle(hebingStyle2);
114         hebingStyle2.setBorderLeft((short)2);
115         cell.setCellStyle(hebingStyle2);
116         cell.setCellValue("合并单元格2");
117         
118         row = sheet.getRow(6);
119         cell = row.getCell(3);
120         cell.setCellStyle(hebingStyle2);
121         
122         // 设定内容输出
123         try {
124             FileOutputStream out = new FileOutputStream(new File("d:\workbook.xls"));
125             workbook.write(out);
126             out.flush();
127             out.close();
128 
129         } catch (IOException e) {
130             e.printStackTrace();
131         }
132         
133 
134     }
原文地址:https://www.cnblogs.com/keyiei/p/3590968.html