There are many ways to export Java tables, such as the form of front-end pop-up dialog boxes, poi operations, and jxl operations. It can be said that there are various forms of implementation. Below I only use one of the poi to operate the Excel table, and at the same time, multiple sheets can be generated in one table, back-end implementation, dynamic designation, and no front-end operation dialog box.
Remember to import the poi related jar package, let's look at the code directly below:
package 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;
public class ExcelUtils {
/**
* @Description: 导出Excel
* @param workbook
* @param sheetNum (sheet的位置,0表示第一个表格中的第一个sheet)
* @param sheetTitle (sheet的名称)
* @param headers (表格的列标题)
* @param result (表格的数据)
* @param out (输出流)
* @throws Exception
*/
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.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
// 生成一个字体
HSSFFont font = workbook.createFont();
font.setColor(HSSFColor.BLACK.index);
font.setFontHeightInPoints((short) 12);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
// 把字体应用到当前的样式
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++;
}
}
}
}
It is also convenient to use:
public static void main(String[] args) {
try {
//excel导出的路径和名称
OutputStream out = new FileOutputStream("D:\\test.xls");
//生成两个sheet,不同的数据源和列标题
List<List<String>> data1 = new ArrayList<List<String>>();
List<List<String>> data2 = new ArrayList<List<String>>();
String[] headers1 = { "ID", "年龄","用户名" };
String[] headers2 = { "ID", "用户名" };
//注意int等其他类型转换成String类型
for (int i = 1; i < 5; i++) {
List rowData = new ArrayList();
rowData.add(String.valueOf(i));
rowData.add(String.valueOf(i+20));
rowData.add("小明"+i+"号");
data1.add(rowData);
}
for (int i = 1; i < 5; i++) {
List rowData = new ArrayList();
rowData.add(String.valueOf(i));
rowData.add("小明"+i+"号");
data2.add(rowData);
}
ExcelUtils eeu = new ExportExcelUtils();
HSSFWorkbook workbook = new HSSFWorkbook();
eeu.exportExcel(workbook, 0, "sheet1", headers1, data1, out);
eeu.exportExcel(workbook, 1, "sheet2", headers2, data2, out);
//将所有的数据一起写入,然后再关闭输入流。
workbook.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
As can be seen from the above call:
(1) The path and name saved by excel can be defined by yourself;
(2) The data source can be obtained from other places, and then assigned and added to the existing data;
(3) The columns in each sheet can be different or the same;
(4) The number of sheets generated depends on you;
Well, the above is a simple and practical excel export method, I hope you like it.