您的位置:首页 > 运维架构 > Apache

Apache POI

2017-01-09 16:23 78 查看
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft
Office格式档案读和写的功能。

结构:

HSSF - 提供读写Microsoft Excel格式档案的功能。

XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。

HWPF - 提供读写Microsoft Word格式档案的功能。

HSLF - 提供读写Microsoft PowerPoint格式档案的功能。

HDGF - 提供读写Microsoft Visio格式档案的功能。

Java代码

package com.sinosoft.Test;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

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.HSSFRichTextString;

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;

public class TestExcel {

public static void main(String[] args) {
HSSFWorkbook workbook = new HSSFWorkbook();// 创建一个Excel文件  
   HSSFSheet sheet = workbook.createSheet();// 创建一个Excel的Sheet  
   sheet.createFreezePane(0, 1);// 冻结  
   // 设置列宽  
   sheet.setColumnWidth(0, 1000);  
   sheet.setColumnWidth(1, 3500);  
   sheet.setColumnWidth(2, 3500);  
   sheet.setColumnWidth(3, 6500);  
   sheet.setColumnWidth(4, 6500);  
   sheet.setColumnWidth(5, 6500);  
   sheet.setColumnWidth(6, 6500);  
   sheet.setColumnWidth(7, 2500);
   sheet.createRow(0);
   sheet.setDefaultRowHeight((short) 2500);
   // 设置字体
   HSSFFont columnHeadFont = workbook.createFont();  
   columnHeadFont.setFontName("隶书");  
   columnHeadFont.setFontHeightInPoints((short) 10);  
   columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
   // 列头的样式  
   HSSFCellStyle columnHeadStyle = workbook.createCellStyle();  
   columnHeadStyle.setFont(columnHeadFont);  
   columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中  
   columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中  
   columnHeadStyle.setLocked(true);  
   columnHeadStyle.setWrapText(true);
   columnHeadStyle.setTopBorderColor(HSSFColor.BLACK.index);// 上边框的颜色  
   columnHeadStyle.setBorderTop((short) 2);// 边框的大小  
   columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色  
   columnHeadStyle.setBorderLeft((short) 2);// 边框的大小  
   columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色  
   columnHeadStyle.setBorderRight((short) 2);// 边框的大小  
   columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_DOUBLE); // 设置单元格的边框为双向 
   columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色  
   // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)  
   columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);  
//创建行
   HSSFRow row2 = sheet.createRow(1);
//写中间部分的字
HSSFCell cell = row2.createCell(0);
cell.setCellValue(new HSSFRichTextString("责任者"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(1);
cell.setCellValue(new HSSFRichTextString("成熟度排序"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(2);
cell.setCellValue(new HSSFRichTextString("事项"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(3);
cell.setCellValue(new HSSFRichTextString("前次会议要求\n/新项目的项目概要"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(4);
cell.setCellValue(new HSSFRichTextString("上周工作进展"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(5);
cell.setCellValue(new HSSFRichTextString("本周工作计划"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(6);
cell.setCellValue(new HSSFRichTextString("问题和建议"));
cell.setCellStyle(columnHeadStyle);
cell = row2.createCell(7);
cell.setCellValue(new HSSFRichTextString("备 注"));
cell.setCellStyle(columnHeadStyle);
try {
FileOutputStream outputStream = new FileOutputStream("e://abc.xls");
workbook.write(outputStream);
outputStream.flush();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: