Apache poi 导出excel实例
以前用过导出excel使用的是jxl.jar,但是目前最高版本只支持excel2003,并不支持excel2007。所以poi支持excel2007,当然也支持excel2003以下各种版本。好了话不多说了,看例子:excel2003我就不多说了,其实jxl就能很好的进行支持,我在这里说一下excel2007.
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public static void main(String[] args) throws Exception {
//输出流
OutputStream os = new FileOutputStream("D:/1.xlsx"); //要保存的excel文件
//工作区
XSSFWorkbook wb = new XSSFWorkbook();
//标题样式
XSSFCellStyle btStyle = wb.createCellStyle();//新建样式对象
XSSFFont btFont = wb.createFont();//创建字体对象
btFont.setFontName("宋体"); //字体
btFont.setBold(true);//是否加粗
btFont.setFontHeightInPoints((short) 15);//字体大小
btStyle.setFont(btFont);
btStyle.setAlignment(XSSFCellStyle.ALIGN_CENTER);//水平居中
btStyle.setVerticalAlignment(XSSFCellStyle.VERTICAL_CENTER);//垂直居中
//卷次样式
XSSFCellStyle juanciStyle = wb.createCellStyle();//新建样式对象
XSSFFont juanciFont = wb.createFont();//创建字体对象
juanciFont.setFontName("宋体"); //字体
juanciFont.setBold(true);//是否加粗
juanciFont.setFontHeightInPoints((short) 13);//字体大小
juanciFont.setColor(new XSSFColor(ColourUtil.getHexToRGB("#7030A0")));//字体颜色
juanciStyle.setFont(juanciFont);
//卷样式
XSSFCellStyle juanStyle = wb.createCellStyle();//新建样式对象
XSSFFont juanFont = wb.createFont();//创建字体对象
juanFont.setFontName("宋体"); //字体
juanFont.setBold(true);//是否加粗
juanFont.setFontHeightInPoints((short) 12);//字体大小
juanFont.setColor(new XSSFColor(ColourUtil.getHexToRGB("#008000")));//字体颜色
juanStyle.setFont(juanFont);
//篇样式
XSSFCellStyle pianStyle = wb.createCellStyle();//新建样式对象
XSSFFont pianFont = wb.createFont();//创建字体对象
pianFont.setFontName("宋体"); //字体
pianFont.setBold(true);//是否加粗
pianFont.setFontHeightInPoints((short) 11);//字体大小
pianFont.setColor(new XSSFColor(ColourUtil.getHexToRGB("#92D050")));//字体颜色
pianStyle.setFont(pianFont);
//章样式
XSSFCellStyle zhangStyle = wb.createCellStyle();//新建样式对象
XSSFFont zhangFont = wb.createFont();//创建字体对象
zhangFont.setFontName("宋体"); //字体
zhangFont.setBold(true);//是否加粗
zhangFont.setFontHeightInPoints((short) 10);//字体大小
zhangFont.setColor(new XSSFColor(ColourUtil.getHexToRGB("#0000FF")));//字体颜色
zhangStyle.setFont(zhangFont);
//节样式
XSSFCellStyle jieStyle = wb.createCellStyle();//新建样式对象
XSSFFont jieFont = wb.createFont();//创建字体对象
jieFont.setFontName("宋体"); //字体
jieFont.setBold(true);//是否加粗
jieFont.setFontHeightInPoints((short) 9);//字体大小
jieFont.setColor(new XSSFColor(ColourUtil.getHexToRGB("#66FFFF")));//字体颜色
jieStyle.setFont(jieFont);
//小节样式
XSSFCellStyle xiaojieStyle = wb.createCellStyle();//新建样式对象
XSSFFont xiaojieFont = wb.createFont();//创建字体对象
xiaojieFont.setFontName("宋体"); //字体
xiaojieFont.setBold(true);//是否加粗
xiaojieFont.setFontHeightInPoints((short) 8);//字体大小
xiaojieFont.setColor(new XSSFColor(ColourUtil.getHexToRGB("#C2AFEB")));//字体颜色
xiaojieStyle.setFont(xiaojieFont);
//校注样式
XSSFCellStyle jiaoazhuStyle = wb.createCellStyle();//新建样式对象
XSSFFont jiaozhuFont = wb.createFont();//创建字体对象
jiaozhuFont.setFontName("宋体"); //字体
jiaozhuFont.setBold(true);//是否加粗
jiaozhuFont.setFontHeightInPoints((short) 8);//字体大小
jiaozhuFont.setColor(new XSSFColor(ColourUtil.getHexToRGB("#0000FF")));//字体颜色
jiaoazhuStyle.setFont(jiaozhuFont);
String[] title = {"史籍语料","ID","唯一标示符号","标题级别","新增条目名","显示标题","主题词","分类"};
//创建第一个sheet
XSSFSheet sheet= wb.createSheet("内容结构");//创建一个工作簿,并起个名字
//生成第一行
XSSFRow row = sheet.createRow(0);
for(int i=0;i<title.length;i++){
Cell cell=row.createCell(i);//取得一行当中的列
cell.setCellValue(title);//设置内容
cell.setCellStyle(btStyle);//设置样式
sheet.setColumnWidth(i, 30*256); // 设置列的宽度
}
//写文件
wb.write(os);
//关闭输出流
os.close();
}
读取excel2007,用poi实现也和简单,看例子:
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public static void main(String[] args) throws Exception {
//office2007工作区
XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("D:/1.xlsx"));
//获得该工作区的第一个sheet
XSSFSheet sheet = wb.getSheetAt(0);
//总共有多少行,从0开始
int totalRows = sheet.getLastRowNum();
for (int i = 0; i <= totalRows; i++) {
//取得该行
XSSFRow row = sheet.getRow(i);
System.out.println(row.getCell(0).toString());//读取该行的第几列,当前是读取该行的地0列。也就是第几个单元格,从0开始
}
}
值得注意的是,XSSFWorkbook支持excel2007,而HSSFWorkbook支持excel2003。这两个类分别支持的。
页:
[1]