package com.cn.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.Region;
public class testExportExcel {
public static void main(String[] args) {
Export();
}
// 不带表头
public static void write1(OutputStream outputStream) throws IOException {
// 初始一个Workbook
HSSFWorkbook workbook = new HSSFWorkbook();
// 第一张表
HSSFSheet sheet = workbook.createSheet("sheetlove");
// 第一行
HSSFRow row = sheet.createRow(0);
// 第一个单元格
HSSFCell cell = row.createCell(0);
// 为第一个单元格赋值
cell.setCellValue("测试1");
// 创建Excel文件
workbook.write(outputStream);
}
// 单行表头简单测试
public static void write2(OutputStream outputStream) throws IOException {
// 初始一个Workbook
HSSFWorkbook workbook = new HSSFWorkbook();
// 第一张表
HSSFSheet sheet = workbook.createSheet("sheetlove");
// 第一行(列)
HSSFRow row = sheet.createRow(0);
// 单元格
HSSFCell cell1 = row.createCell(0);
HSSFCell cell2 = row.createCell(1);
HSSFCell cell3 = row.createCell(2);
// 为单元格赋值
cell1.setCellValue("编号");
cell2.setCellValue("姓名");
cell3.setCellValue("性别");
// 创建Excel文件
workbook.write(outputStream);
}
// 复合表头简单测试
public static void write3(OutputStream outputStream) throws IOException {
//初始一个workbook
HSSFWorkbook workbook = new HSSFWorkbook();
//获取第一张
HSSFSheet sheet = workbook.createSheet("合并单元格");
//创建合并单元格的第一个单元格数据
HSSFRow row = sheet.createRow(0); HSSFCell c0 = row.createCell(0);
c0.setCellValue(new HSSFRichTextString("工号"));
HSSFCell c1 = row.createCell(1);
c1.setCellValue(new HSSFRichTextString("姓名"));
HSSFCell c2 = row.createCell(2);
c2.setCellValue(new HSSFRichTextString("部门"));
HSSFCell c3 = row.createCell(3);
c3.setCellValue(new HSSFRichTextString("2010年度休假数据"));
HSSFCell c4 = row.createCell(7);
c4.setCellValue(new HSSFRichTextString("2011年度休假数据"));
HSSFRow row1 = sheet.createRow(1); HSSFCell c5 = row1.createCell(3);
c5.setCellValue(new HSSFRichTextString("2010法定总假(天)"));
HSSFCell c6 = row1.createCell(4);
c6.setCellValue(new HSSFRichTextString("2010弹性总假(天)"));
HSSFCell c7 = row1.createCell(5);
c7.setCellValue(new HSSFRichTextString("2010病假总假(天)"));
HSSFCell c8 = row1.createCell(6);
c8.setCellValue(new HSSFRichTextString("2010补充总假(天)"));
HSSFCell c9 = row1.createCell(7);
c9.setCellValue(new HSSFRichTextString("2011法定总假(天)"));
HSSFCell c10 = row1.createCell(8);
c10.setCellValue(new HSSFRichTextString("2011弹性总假(天)"));
HSSFCell c11 = row1.createCell(9);
c11.setCellValue(new HSSFRichTextString("2011病假总假(天)"));
HSSFCell c12 = row1.createCell(10);
c12.setCellValue(new HSSFRichTextString("2011补充总假(天)"));
//设置合并单元格的区域
Region region1 = new Region(0, (short)0, 1, (short)0);
Region region2 = new Region(0, (short)1, 1, (short)1);
Region region3 = new Region(0, (short)2, 1, (short)2);
Region region4 = new Region(0, (short)3, 0, (short)6);
Region region5 = new Region(0, (short)7, 0, (short)10);
sheet.addMergedRegion(region1);
sheet.addMergedRegion(region2);
sheet.addMergedRegion(region3);
sheet.addMergedRegion(region4);
sheet.addMergedRegion(region5);
// 创建Excel文件
workbook.write(outputStream);
}
// 通用新建Excel
public static void Export() {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(new File(
"E:\\testExcel\\helloPOI3.xls"));
//自己换测试类
write3(outputStream);
System.out.println("创建成功,请查看效果!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 1
- 2
- 3
前往页