java excel导出 poi 后缀名_Java使用poi生成Excel

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();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值