Java 使用哈希映射写入Excel文件 嗨,我从哈希映射中得到了精确的值,但是我的ApachePOI行和 单元格无法正确设置值预期结果是这样的请让我 我知道,谢谢
我得到的结果哈希映射如下:Java 使用哈希映射写入Excel文件 嗨,我从哈希映射中得到了精确的值,但是我的ApachePOI行和 单元格无法正确设置值预期结果是这样的请让我 我知道,谢谢,java,apache-poi,Java,Apache Poi,我得到的结果哈希映射如下: {1=[ACSS Description1, ACSS Description2, ACSS Description3, SACSS Description4], 2=[11, 1, 4, 12]} 我期待着: 我得到的结果基于以下代码: 这就是我的代码: public void getList(List<ExportReport> listcriteria) { Map<Integer, List<Stri
{1=[ACSS Description1, ACSS Description2, ACSS Description3, SACSS Description4], 2=[11, 1, 4, 12]}
我期待着:
我得到的结果基于以下代码:
这就是我的代码:
public void getList(List<ExportReport> listcriteria)
{
Map<Integer, List<String>> hashmap = new HashMap<Integer , List<String>>();
List<String> listpropertyvalue =new ArrayList<String>();
for(int i=0; i < listcriteria.size(); i++)
{
String strValue =listcriteria.get(i).getDescription();
listpropertyvalue.add(strValue);
hashmap.put(1, listpropertyname);
}
listpropertyvalue =new ArrayList<String>();
for(int i=0;i<listcriteria.size();i++){
String strInterValue=listcriteria.get(i).getExportIntervalId().toString();
listpropertyvalue.add(strInterValue);
hashmap.put(2, listpropertvalue);
}
}
Set<Integer> keyset = hashmap.keySet();
int rownum = 1;
int cellnum = 0
for(Integer key : keyset){
Row row = worksheet.createRow(rownum++);
Cell cell = row.createCell(cellnum);
List<String> nameList = hashmap.get(key);
for(Object obj : nameList)
{
if(obj instanceof Date)
{
cell.setCellValue((Date) obj);
}
else if(obj instanceof Boolean)
{
cell.setCellValue((Boolean) obj);
}
else if(obj instanceof String)
{
cell.setCellValue((String) obj);
}
else if(obj instanceof Double)
{
cell.setCellValue((Double) obj);
}
}
cellnum++;
rownum=1;
}
}
public void getList(列表条件)
{
Map hashmap=新hashmap();
List listpropertyvalue=new ArrayList();
对于(int i=0;i 对于(inti=0;我很抱歉这么晚才回复
使用您的代码,我创建了一个工作程序,用于初始化Excel文件Writesheet.xlsx,每行包含5个单元格。此外,我还创建了一个包含5个字符串的列表。然后,我使用getList(List listcriteria)
方法将此列表的内容写入Writesheet.xlsx
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
static XSSFWorkbook workbook = new XSSFWorkbook();
public void getList(List<String> listcriteria){
Map<Integer, List<String>> hashmap = new HashMap<Integer , List<String>>();
//create 5 key value pairs
for(int i=0; i < 5; i++){
hashmap.put(i, listcriteria);
}
System.out.println("hashmap : "+hashmap);
Set<Integer> keyset = hashmap.keySet();
int rownum = 0;
int cellnum = 0;
XSSFSheet sheet = workbook.getSheetAt(0);
rownum = 0;
for(Integer key : keyset){
List<String> nameList = hashmap.get(key);
for(String s : nameList){
XSSFRow row = sheet.getRow(rownum++);
Cell cell = row.getCell(cellnum);
if(null!=cell){
cell.setCellValue(s);
}
}
cellnum++;
rownum=0;
}
}
public static void main(String[] args) throws IOException {
//Creation of List from an Array to test getList Method
String[] ss = {"a","b","c","d","e"};
List<String> listcriteria = new ArrayList<String>();
listcriteria.addAll(Arrays.asList(ss));
/***********************************************************/
Test t = new Test();
// Because I put 5 key values pairs in hashmap (see getList method), I create Writesheet.xlsx
// file that contains 5 rows each row contains 5 cell
FileOutputStream out = new FileOutputStream( new File("Writesheet.xlsx"));
XSSFSheet sheet = workbook.createSheet();
for(int i = 0;i<5;i++){
XSSFRow row = sheet.createRow(i);
for(int j=0;j<5;j++)
row.createCell(j);
}
workbook.write(out);
out.close();//end creation of Excel file
// I open Writesheet.xlsx file and write the data on it
InputStream inp = new FileInputStream( new File("Writesheet.xlsx"));
workbook = new XSSFWorkbook(inp);
// listcriteria contains the data that will be written it on Writesheet.xlsx
t.getList(listcriteria);
out = new FileOutputStream( new File("Writesheet.xlsx"));
workbook.write(out);
out.close();
inp.close();
System.out.println("Writesheet.xlsx written successfully" );
}
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公开课考试{
静态XSSF工作簿=新XSSF工作簿();
public void getList(列表条件){
Map hashmap=新hashmap();
//创建5个键值对
对于(int i=0;i<5;i++){
hashmap.put(i,listcriteria);
}
System.out.println(“hashmap:+hashmap”);
Set keyset=hashmap.keyset();
int rownum=0;
int-cellnum=0;
XSSFSheet sheet=workbook.getSheetAt(0);
rownum=0;
for(整数键:键集){
List nameList=hashmap.get(key);
用于(字符串s:名称列表){
XSSFRow row=sheet.getRow(rownum++);
Cell Cell=row.getCell(cellnum);
如果(空!=单元格){
单元格。设置单元格值;
}
}
cellnum++;
rownum=0;
}
}
公共静态void main(字符串[]args)引发IOException{
//从数组创建列表以测试getList方法
字符串[]ss={“a”、“b”、“c”、“d”、“e”};
List listcriteria=新建ArrayList();
addAll(Arrays.asList(ss));
/***********************************************************/
测试t=新测试();
//因为我在hashmap中放入了5个键值对(参见getList方法),所以我创建了Writesheet.xlsx
//包含5行的文件每行包含5个单元格
FileOutputStream out=新的FileOutputStream(新文件(“Writesheet.xlsx”);
XSSFSheet sheet=workbook.createSheet();
对于(int i=0;i我只需更改void getList(List listcriteria)
上的代码,以便
{1=[ACSS描述1,ACSS描述2,ACSS描述3,SACSS
描述4],2=[11,1,4,12]}
剩下的代码还是一样的,瞧,你在Writesheet.xlsx上得到了你想要的
Map<Integer, List<String>> hashmap = new HashMap<Integer , List<String>>();
String[] data1 = {"ACSS Description1", "ACSS Description2", "ACSS Description3", "SACSS Description4"};
List s1 = Arrays.asList(data1);
hashmap.put(1,s1);
String[] data2 = {"11", "1", "4", "12"};
List s2 = Arrays.asList(data2);
hashmap.put(2,s2);
System.out.println("hashmap : "+hashmap);
//the rest of code it is the same
Map hashmap=new hashmap();
字符串[]数据1={“ACSS描述1”、“ACSS描述2”、“ACSS描述3”、“SACSS描述4”};
List s1=Arrays.asList(data1);
hashmap.put(1,s1);
字符串[]数据2={“11”、“1”、“4”、“12”};
List s2=Arrays.asList(data2);
hashmap.put(2,s2);
System.out.println(“hashmap:+hashmap”);
//代码的其余部分是相同的
所有代码都有更改
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Test {
static XSSFWorkbook workbook = new XSSFWorkbook();
public void getList(List<String> listcriteria){
Map<Integer, List<String>> hashmap = new HashMap<Integer , List<String>>();
String[] data1 = {"ACSS Description1", "ACSS Description2", "ACSS Description3", "SACSS Description4"};
List s1 = Arrays.asList(data1);
hashmap.put(1,s1);
String[] data2 = {"11", "1", "4", "12"};
List s2 = Arrays.asList(data2);
hashmap.put(2,s2);
System.out.println("hashmap : "+hashmap);
Set<Integer> keyset = hashmap.keySet();
int rownum = 1;
int cellnum = 0;
XSSFSheet sheet = workbook.getSheetAt(0);
for(Integer key : keyset){
List<String> nameList = hashmap.get(key);
for(String s : nameList){
XSSFRow row = sheet.getRow(rownum++);
Cell cell = row.getCell(cellnum);
if(null!=cell){
cell.setCellValue(s);
}
}
cellnum++;
rownum=1;
}
}
public static void main(String[] args) throws IOException {
//Creation of List from an Array to test getList Method
String[] ss = {"a","b","c","d","e"};
List<String> listcriteria = new ArrayList<String>();
listcriteria.addAll(Arrays.asList(ss));
/***********************************************************/
Test t = new Test();
// Because I put 5 key values pairs in hashmap (see getList method), I create Writesheet.xlsx
// file that contains 5 rows each row contains 5 cell
FileOutputStream out = new FileOutputStream( new File("Writesheet.xlsx"));
XSSFSheet sheet = workbook.createSheet();
for(int i = 0;i<5;i++){
XSSFRow row = sheet.createRow(i);
for(int j=0;j<5;j++)
row.createCell(j);
}
workbook.write(out);
out.close();//end creation of Excel file
// I open Writesheet.xlsx file and write the data on it
InputStream inp = new FileInputStream( new File("Writesheet.xlsx"));
workbook = new XSSFWorkbook(inp);
// listcriteria contains the data that will be written it on Writesheet.xlsx
t.getList(listcriteria);
out = new FileOutputStream( new File("Writesheet.xlsx"));
workbook.write(out);
out.close();
inp.close();
System.out.println("Writesheet.xlsx written successfully" );
}
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.Set;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公开课考试{
静态XSSF工作簿=新XSSF工作簿();
public void getList(列表条件){
Map hashmap=新hashmap();
字符串[]数据1={“ACSS描述1”、“ACSS描述2”、“ACSS描述3”、“SACSS描述4”};
List s1=Arrays.asList(data1);
hashmap.put(1,s1);
字符串[]数据2={“11”、“1”、“4”、“12”};
List s2=Arrays.asList(data2);
hashmap.put(2,s2);
System.out.println(“hashmap:+hashmap”);
Set keyset=hashmap.keyset();
int rownum=1;
int-cellnum=0;
XSSFSheet sheet=workbook.getSheetAt(0);
for(整数键:键集){
List nameList=hashmap.get(key);
用于(字符串s:名称列表){
XSSFRow row=sheet.getRow(rownum++);
Cell Cell=row.getCell(cellnum);
如果(空!=单元格){
单元格。设置单元格值;
}
}
cellnum++;
rownum=1;
}
}
公共静态void main(字符串[]args)引发IOException{
//从数组创建列表以测试getList方法
字符串[]ss={“a”、“b”、“c”、“d”、“e”};
List listcriteria=新建ArrayList();
addAll(Arrays.asList(ss));
/***********************************************************/
测试t=新测试();
//因为我在hashmap中放入了5个键值对(参见getList方法),所以我创建了Writesheet.xlsx
//包含5行的文件,每行包含