Java 循环过程中Excel列的分布行为,是否可能?
使用ApachePOI3.9,我想将一些值映射到一个Excel模板,该模板类似于以下内容: 看起来很简单,至少对于卖方表来说是这样,在这个表中,我可以毫无问题地映射bean中的值 然而,我面临的问题与Products表有关,因为我需要为每个列设置bean中的数据,这使得逻辑有点复杂,因为在循环过程中,我需要查找下一个列字母: 例如我的第一个产品数据将在B7下设置,然后在C7、D7、E7等处设置,直到循环结束 (要知道,对于这个示例模板,我只是显示产品的“Name”属性,但在现实生活中,每个属性都有大约35个属性,这就是为什么我不以行的形式显示数据,因为在水平视图中,表对用户来说不太友好) 所以,我的问题是: 如果我的产品数量超过产品的总字母数,会发生什么 字母表,如何在循环过程中获得正确的列和单元格 按照Excel列分布设置我的产品bean数据 “Excel列分布”指的是以下内容: 例如,在Excel中,当转到包含字母表“Z”最后一个字母的列时,这些列继续显示AA、AB、AC等 可能吗? 这是我尝试过的(使用虚拟数据),在进入字母“Z”列之前,这将一直有效: 此代码段中使用的空Excel模板可在以下位置下载:Java 循环过程中Excel列的分布行为,是否可能?,java,apache-poi,export-to-excel,Java,Apache Poi,Export To Excel,使用ApachePOI3.9,我想将一些值映射到一个Excel模板,该模板类似于以下内容: 看起来很简单,至少对于卖方表来说是这样,在这个表中,我可以毫无问题地映射bean中的值 然而,我面临的问题与Products表有关,因为我需要为每个列设置bean中的数据,这使得逻辑有点复杂,因为在循环过程中,我需要查找下一个列字母: 例如我的第一个产品数据将在B7下设置,然后在C7、D7、E7等处设置,直到循环结束 (要知道,对于这个示例模板,我只是显示产品的“Name”属性,但在现实生活中,每个属性
package com.app.test;
导入java.io.File;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.util.ArrayList;
导入java.util.LinkedHashMap;
导入java.util.List;
导入java.util.Map;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellStyle;
导入org.apache.poi.ss.usermodel.IndexedColors;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.ss.util.CellReference;
/**
*用于生成Excel工作表的测试类
*/
公共类导出测试
{
公共静态void main(字符串[]args)引发IOException
{
参考文献;
//加载模板
InputStream fis=ExportTest.class.getResourceAsStream(“/template.xls”);
工作簿=新的HSSF工作簿(fis);
fis.close();
//常数
最终字符串TBL_FIRSTCOLUMN=“B”//在工作表中开始产品表列(其中将设置第一个产品数据)
final int MAX_PRODUCTS=25;//添加到虚拟产品列表的最大产品数(这将最后一个产品设置为“Z”列)
final int TBL_STARTROW=7;//在工作表处开始产品表行号(其中将设置第一个产品数据)
final int TBL_ATTR_ROWS=1;//products表中的属性行数(在本例中仅为“Name”)
//使用卖家信息生成虚拟数据
LinkedHashMap cellMap=新LinkedHashMap();
cellMap.put(“B2”、“1”);
cellMap.put(“B3”、“公司”);
cellMap.put(“B4”、“美国”);
//使用产品信息生成虚拟数据
列表产品=新的ArrayList();
对于(int i=0;ipackage com.app.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellReference;
/**
* Test class for generating Excel sheet
*/
public class ExportTest
{
public static void main(String[] args) throws IOException
{
CellReference ref;
// Load template
InputStream fis = ExportTest.class.getResourceAsStream("/template.xls");
Workbook workbook = new HSSFWorkbook(fis);
fis.close();
// Constants
final String TBL_FIRSTCOLUMN = "B"; // Starting product table column at sheet (in which the first product data will be set)
final int MAX_PRODUCTS = 25; // Max. products added to the dummy products list (this will set the last product to the "Z" column)
final int TBL_STARTROW = 7; // Starting product table row number at sheet (in which the first product data will be set)
final int TBL_ATTR_ROWS = 1; // Number of attribute rows at products table (in this case just "Name")
// Generate dummy data with seller information
LinkedHashMap<String, String> cellMap = new LinkedHashMap<String, String>();
cellMap.put("B2", "1");
cellMap.put("B3", "Company");
cellMap.put("B4", "US");
// Generate dummy data with product information
List<String> products = new ArrayList<String>();
for(int i = 0; i < MAX_PRODUCTS; ++i) {
products.add("Chocolate");
}
// Declare style for cells
CellStyle style = workbook.createCellStyle();
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderRight(CellStyle.BORDER_THIN);
style.setRightBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderTop(CellStyle.BORDER_THIN);
style.setTopBorderColor(IndexedColors.BLACK.getIndex());
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
// Get template sheet
Sheet sheet = workbook.getSheetAt(0);
// Set values to "Seller" table
for(Map.Entry<String, String> entry : cellMap.entrySet()) {
ref = new CellReference(entry.getKey());
sheet.getRow(ref.getRow()).getCell(ref.getCol()).setCellValue(entry.getValue());
}
// Set values to "Products" table
for(int i = 0; i < products.size(); ++i) {
// Get product name
String name = products.get(i);
String num = String.valueOf(i + 1);
// Get char representation of the letter, this will allow me to get
// C, D, E...Z columns but then I will get a IllegalArgumentException
// if my products count exceed the alphabet letters. At this point I'll
// need to follow Excel column distribution behavior.
String nextLetter = String.valueOf((char)(TBL_FIRSTCOLUMN.charAt(0) + i));
for(int j = 0; j < TBL_ATTR_ROWS; ++j) {
// Get cell reference of B7 then C7, etc
ref = new CellReference(nextLetter + (TBL_STARTROW + j));
// Check if row/cell exists, otherwise it will throw NullPointerException when trying to get each one
Row row = sheet.getRow(ref.getRow());
if(row == null) {
row = sheet.createRow(ref.getRow());
}
Cell cell = row.getCell(ref.getCol());
if(cell == null) {
cell = row.createCell(ref.getCol());
}
// Set value and style to cell
cell.setCellValue(name + num);
cell.setCellStyle(style);
}
}
// Write workbook to file
String path = String.format("%s%s%s", System.getProperty("user.home"), System.getProperty("file.separator"), "exported.xls");
OutputStream out = new FileOutputStream(new File(path));
workbook.write(out);
out.close();
}
}
String nextLetter = String.valueOf(CellReference.convertNumToColString(i + 1));