java导出数据excel生成多个sheet
作者:互联网
--实际开发中根据功能需要,导出数据到excel需要分多页显示,以下实现方法记录下来方便记忆。
package com.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelExportTest {
public static void main(String[] args) {
try {
File f= new File("d:" + File.separator + "test.xls") ;
OutputStream out = new FileOutputStream(f);//导出本地
//OutputStream out = response.getOutputStream();//输出客户端
List<List<String>> data1 = new ArrayList<List<String>>();
List<List<String>> data2 = new ArrayList<List<String>>();
List<List<String>> data3 = new ArrayList<List<String>>();
for (int i = 1; i < 5; i++) {
List rowData = new ArrayList();
rowData.add(String.valueOf(i));
rowData.add("上海东霖柏鸿");
data1.add(rowData);
}
for (int i = 1; i < 5; i++) {
List rowData = new ArrayList();
rowData.add(String.valueOf(i));
rowData.add("深圳东霖柏鸿");
data2.add(rowData);
}
for (int i = 1; i < 5; i++) {
List rowData = new ArrayList();
rowData.add(String.valueOf(i));
rowData.add("广州东霖柏鸿");
data3.add(rowData);
}
String[] headers1 = { "ID1", "用户名1" };
String[] headers2 = { "ID2", "用户名2" };
String[] headers3 = { "ID3", "用户名3" };
ExportExcelUtilsTest eeu = new ExportExcelUtilsTest();//工具类写法在下面
HSSFWorkbook workbook = new HSSFWorkbook();
eeu.exportExcel(workbook, 0, "上海", headers1, data1, out);
eeu.exportExcel(workbook, 1, "深圳", headers2, data2, out);
eeu.exportExcel(workbook, 2, "广州", headers3, data3, out);
//原理就是将所有的数据一起写入,然后再关闭输入流。
workbook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.test;
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.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;
/**
* @Title: exportExcel
* @Description: 导出Excel的方法
* @author: liuct @ 2019-01-29
* @param workbook
* @param sheetNum (sheet的位置,0表示第一个表格中的第一个sheet)
* @param sheetTitle (sheet的名称)
* @param headers (表格的标题)
* @param result (表格的数据)
* @param out (输出流)
* @throws Exception
*/
public class ExportExcelUtilsTest {
public void exportExcel(HSSFWorkbook workbook, int sheetNum,
String sheetTitle, String[] headers, List<List<String>> result,
OutputStream out) throws Exception {
// 生成一个表格
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(sheetNum, sheetTitle);
// 设置表格默认列宽度为20个字节
sheet.setDefaultColumnWidth((short) 20);
// 生成一个样式
HSSFCellStyle style = workbook.createCellStyle();
// 设置这些样式
style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
style.setFillBackgroundColor(HSSFColor.WHITE.index);
style.setFillForegroundColor(HSSFColor.LIGHT_ORANGE.index);
// 生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 12);
// 把字体应用到当前的样式
style.setFont(font);
// 指定当单元格内容显示不下时自动换行
style.setWrapText(true);
// 产生表格标题行
HSSFRow row = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell((short) i);
cell.setCellStyle(style);
HSSFRichTextString text = new HSSFRichTextString(headers[i]);
cell.setCellValue(text.toString());
}
// 遍历集合数据,产生数据行
if (result != null) {
int index = 1;
for (List<String> m : result) {
row = sheet.createRow(index);
int cellIndex = 0;
for (String str : m) {
HSSFCell cell = row.createCell((short) cellIndex);
cell.setCellValue(str.toString());
cellIndex++;
}
index++;
}
}
}
}
标签:java,String,List,excel,rowData,new,workbook,import,sheet 来源: https://www.cnblogs.com/weiyi1314/p/11686018.html