写第二行数据:
for (int i = 0; i < dictInfo.size(); i++) {
HSSFRow dataRow = sheet.createRow(i + 1);
dataRow.setHeight((short) (15 * 20));
JavaList l = (JavaList) dictInfo.getKeys();
String s = (String) l.get(i);
JavaList ll = (JavaList) dictInfo.get(s); //获取列数据
for (int j = 0; j < ll.size(); j++) {
Cell dataCell = dataRow.createCell(j);
HSSFFont dataFont = wb.createFont();
dataFont.setFontName("华文细黑");
dataFont.setFontHeightInPoints((short) 10);
dataFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
HSSFCellStyle dataStyle = wb.createCellStyle();
dataStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
dataStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
dataStyle.setFont(dataFont);
dataCell.setCellStyle(dataStyle);
dataCell.setCellValue(ll.get(j).toString());
}
}
dictInfo中数据:
{
"ordercode":["订单号0","订单号1"],
"tradeno":["交易号0","交易号1"],
"procode":["技术产品代码0","技术产品代码"],
"proname":["技术产品名称0","技术产品名称1"]
}
用二维数组解决问题:
int dictsize = dictInfo.size();
int listsize = ((JavaList) dictInfo.get(dictInfo.getKeys().get(0))).size();
//数据处理
String[][] parameter = new String[dictsize][listsize];
for (int i = 0; i < dictsize; i++) {
JavaList list = (JavaList) dictInfo.getKeys();
JavaList listInfo = (JavaList) dictInfo.get(list.get(i)); //获取列数据
for (int j = 0; j < listsize; j++) {
parameter[i][j] = listInfo.get(j).toString();
}
}
//写第二行……
for (int i = 0; i < listsize; i++) {
HSSFRow dataRow = sheet.createRow(i + 1);
dataRow.setHeight((short) (15 * 20));
for (int j = 0; j < dictsize; j++) {
Cell dataCell = dataRow.createCell(j);
HSSFFont dataFont = wb.createFont();
dataFont.setFontName("华文细黑");
dataFont.setFontHeightInPoints((short) 10);
dataFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
HSSFCellStyle dataStyle = wb.createCellStyle();
dataStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
dataStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
dataStyle.setFont(dataFont);
dataCell.setCellStyle(dataStyle);
dataCell.setCellValue(parameter[j][i]);
}
}
listInfo和JavaList I 顺序反了吧,你这是逻辑错误吧,自己debug一下不就知道了
刚刚上传错了代码,l是需要从dictInfo中取数据
可以试试Free Spire.XLS for Java
将数组写入Excel教程:https://www.e-iceblue.cn/spirexls_java_data/insert-arrays-to-excel-in-java.html , 当然也能设置文字对齐方式和加粗
jar属于付费版,这个企业没购买肯定是不能使用的
@借你耳朵说爱你: 可用的哈,这个免费版只是对格式转换做了限制,正常生成、处理excel文档是没问题的