[JAVA] exportation courante pour exceller en utilisant poi

package com.sql.main; import org.apache.poi.hssf.usermodel.; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.; import org.apache.poi.ss.util.CellRangeAddress;

import java.io.*; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map;

/**

//最新Excel列索引, // int lastRowIndex = sheet.getLastRowNum(); // if (lastRowIndex > 0) { // lastRowIndex++; // } int cellCount =0; Row titleRow = sheet.createRow(0); for(Field f : cls.getDeclaredFields()){ String cellName = f.getName(); Cell cell =titleRow.createCell(cellCount); cell.setCellValue(cellName.toUpperCase()); cellCount++; } int lastRowIndex=1; // sheet.setDefaultColumnWidth(DEFAULT_COLUMN_SIZE); for (Object dataRow : contents) { Row row = sheet.createRow(lastRowIndex); Object bean =cls.newInstance(); int cellIndex =0; for(Field f : cls.getDeclaredFields()){ String getMethodName = "get"+f.getName().substring(0, 1).toUpperCase()+f.getName().substring(1); Method getMethod =cls.getMethod(getMethodName, null ); String value = getMethod.invoke(dataRow)==null ? "NULL":getMethod.invoke(dataRow).toString(); Cell cell =row.createCell(cellIndex); cell.setCellValue(value); cellIndex++; } lastRowIndex++; } OutputStream ops = new FileOutputStream(file); workBook.write(ops); ops.flush(); ops.close(); System.out.println("sheet:"+sheetName+" write "+lastRowIndex+" success!"); } catch (Exception e) { e.printStackTrace(); } }

}

Recommended Posts

exportation courante pour exceller en utilisant poi
Opération Excel avec Apache POI
Sortie vers Excel en utilisant Apache POI!
Comment sortir Excel et PDF avec Excella
[Java] Création d'un fichier Excel à l'aide d'Apache POI
[Apache POI] Version Excel correspondante
Comment utiliser Apache POI
Manipuler Excel avec Apache POI
Apache POI Excel avec Kotlin
Comment autoriser à l'aide de graphql-ruby