本文共 3235 字,大约阅读时间需要 10 分钟。
package test.cc;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
public class poi {
public static String[] headers={"第一个","第二个","第三个"};
public static String[] data1={"数据1","数据11","数据111"};
public static String[] data2={"数据2","数据22","数据222"};
public static List dataset=new ArrayList(){
{add(data1);
add(data2);
}
};
public static void main(String[] args) throws IOException {
OutputStream os=new FileOutputStream("test.xlsx");
// 声明一个工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
try {
// 生成一个表格
HSSFSheet sheet = workbook.createSheet("testtest");
// 生成一个样式
HSSFCellStyle headerStyle = workbook.createCellStyle();
// 设置这些样式
headerStyle.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
headerStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
headerStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
HSSFFont headerFont = workbook.createFont();
headerFont.setColor(HSSFColor.VIOLET.index);
headerFont.setFontHeightInPoints((short) 12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
headerStyle.setFont(headerFont);
// 生成并设置另一个样式
HSSFCellStyle dataStyle = workbook.createCellStyle();
dataStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
dataStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
dataStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
dataStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
dataStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
dataStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
dataStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
dataStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
// 生成另一个字体
HSSFFont dataFont = workbook.createFont();
dataFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
// 把字体应用到当前的样式
dataStyle.setFont(dataFont);
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
HSSFCell noCell = row.createCell(0);
noCell.setCellStyle(headerStyle);
noCell.setCellValue("编号");
for (int headerIndex = 0; headerIndex < headers.length; headerIndex++) {
HSSFCell headerCell = row.createCell(headerIndex + 1);
headerCell.setCellStyle(headerStyle);
HSSFRichTextString text = new HSSFRichTextString(headers[headerIndex]);
headerCell.setCellValue(text);
}
// 遍历集合数据,产生数据行
for (int dataIndex = 0; dataIndex < dataset.size(); dataIndex++) {
row = sheet.createRow(dataIndex + 1);
HSSFCell dataNoCell = row.createCell(0);
dataNoCell.setCellStyle(dataStyle);
dataNoCell.setCellValue(dataIndex + 1);
String[] rowData = dataset.get(dataIndex);
for (int fieldIndex = 0; fieldIndex < rowData.length; fieldIndex++) {
HSSFCell dataCell = row.createCell(fieldIndex + 1);
dataCell.setCellStyle(dataStyle);
dataCell.setCellValue(rowData[fieldIndex]);
}
}
for (int columnIndex = 0; columnIndex < headers.length + 1; columnIndex++) {
sheet.autoSizeColumn(columnIndex, true);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 清理资源
workbook.write(os);
workbook.close();
}
}
}
转载地址:http://bodnv.baihongyu.com/