HSSF或这是POI到出excel表格

来源:互联网 发布:淘宝店铺转让骗局 编辑:程序博客网 时间:2024/05/21 10:15
package com.text;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFFont;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;/** * ClassName: MyTest  * @Description: example of java exporting excel file; * @author mpc * @date 2016年7月7日 */public class MyTest {public static void main(String[] args) {HSSFWorkbook book = new HSSFWorkbook();// 这个相当于excel文件HSSFSheet sheet = book.createSheet("第一个");// 这个相当于excel中的一个sheetHSSFRow row;// excel中的行HSSFCell cell;// excel中的单元格HSSFCellStyle style = book.createCellStyle();// 单元格的样式// 背景色style.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);// 前景色style.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);// 填充模式style.setFillPattern(HSSFCellStyle.SQUARES);// 这个是前景色和背景色的填充样式style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 对齐方式(水平)style.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);// 对齐方式(垂直)// 设置上下左右边框样式style.setBorderLeft(HSSFCellStyle.BORDER_THIN);style.setBorderBottom(HSSFCellStyle.BORDER_THIN);style.setBorderRight(HSSFCellStyle.BORDER_THIN);style.setBorderTop(HSSFCellStyle.BORDER_THIN);// 自动换行style.setWrapText(true);// 设置字体HSSFFont font = book.createFont();font.setFontName("黑体");font.setFontHeightInPoints((short) 22);font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);String[] headRow1 = { "序号", "学校", "", "班级信息", "", "个人信息", "", "","综合考评" };String[] headRow2 = { "", "名称", "位置", "年级", "几班", "姓名", "性别", "联系方式","" };// 在创建row的时候,没一行的row中的每一个单元格都必须有数据。没用的合并就可以了。int rowint = 0;int titlerow1 = rowint++;// 第0行System.out.println(titlerow1);row = sheet.createRow((short) titlerow1);// 在sheet中创建第一行for (int i = 0; i < headRow1.length; i++) {// 给第一行中的每个cell中写入数据cell = row.createCell(i);cell.setCellValue(headRow1[i]);cell.setCellStyle(style);}int titlerow2 = rowint++;row = sheet.createRow(titlerow2);// 在sheet中创建第二行for (int i = 0; i < headRow2.length; i++) {// 给第二行的每个cell写入数据cell = row.createCell(i);cell.setCellValue(headRow2[i]);cell.setCellStyle(style);}// 合并单元格sheet.addMergedRegion(new CellRangeAddress(titlerow1, titlerow1, 1, 2));sheet.addMergedRegion(new CellRangeAddress(titlerow1, titlerow1, 3, 4));sheet.addMergedRegion(new CellRangeAddress(titlerow1, titlerow1, 5, 7));sheet.addMergedRegion(new CellRangeAddress(titlerow1, titlerow2, 8, 8));// 设置每列的宽度for (int k = 0; k < headRow1.length; k++) {sheet.setColumnWidth(k, 4200);}// 放入信息,一般为list,这里用模拟信息String message[] = { "1", "清华", "北京", "计算机", "我爱你我爱你我爱你我爱你我爱你我爱你","snow", "女", "562771681", "有" };List<String[]> list = new ArrayList<String[]>();list.add(message);titlerow2++;for (int i = 0; i < list.size(); i++) {// 生成一行数据String[] mess = (String[]) list.get(i);row = sheet.createRow(titlerow2 + i);for (int kk = 0; kk < mess.length; kk++) {cell = row.createCell(kk);cell.setCellStyle(style);cell.setCellValue(mess[kk]);}}FileOutputStream outputStream;try {// 输出文件outputStream = new FileOutputStream("e://mpc.xls");book.write(outputStream);outputStream.flush();outputStream.close();//book.getBytes(),转换为byte[]数组,在网络上传播的时候可用} catch (Exception e) {e.printStackTrace();} finally {System.exit(0);}}}



运行结果:


1 0
原创粉丝点击