从HashMap中以java创建CSV文件 [英] Creating a CSV File in java from a HashMap
问题描述
我有一个hashMap在java中的一些键,每个键指示一个流。然后每个显示每个数据包的静态的值都属于该流程。
我需要做的是,基于这些值为每个流绘制图形。例如:
Flow1:{[length,time],[],[],...}
Flow2:{[length,time],[length,time],[],...}
我需要创建一个CSV文件,然后可以从MS Excel中读取。
编辑:
这里是我的hashMap:
迭代器< Flows> iterator = myHashMap.keySet()。iterator();
String fileName =((args.length> 0)?args [0]:jexcel.xls);
Map< String,ArrayList> csv = new HashMap< String,ArrayList>();
int i = 0;
while(iterator.hasNext()){
Flows键= iterator.next();
ArrayList value = myHashMap.get(key);
csv.put(Flow [+ i +],value);
}
如果您真的想要一个Excel文件,创建一个最好的库是 Andy Khan的JExcel 。 p>
我认为每个流程需要一个工作表,每个流有.csv对,按时间排序。
如果这些是变量对时间的图形,那么时间不是每对中的第一个值?
这是我怎么做的。它完美地适用于我提供的简单测试用例 - 它是可以扩展的工作代码。
包jexcel;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JExcelUtils
{
public static void main(String [] args)
{
String fileName =((args.length> 0 )?args [0]:jexcel.xls);
Map< String,List&Pair>> csv = new HashMap< String,List&Pair>>()
{{
put(Flow1,fromArrayToList(new double [] []
{
{0.0 ,0.0},
{0.1,1.0},
{0.2,2.0},
{0.3,3.0},
{0.4,4.0},
{0.5 ,5.0},
}));
put(Flow2,fromArrayToList(new double [] []
{
{0.0,0.0},
{0.1,1.0},
{ 4.0},
{0.3,9.0},
{0.4,16.0},
{0.5,25.0},
}
}};
WritableWorkbook excelContents = null;
try
{
文件excelFile =新文件(fileName);
excelContents = createExcel(excelFile,csv);
excelContents.write();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(WriteException e)
{
e.printStackTrace();
}
finally
{
try {if(excelContents!= null)excelContents.close(); } catch(Exception e){e.printStackTrace(); }
}
}
public static List< Pair> fromArrayToList(double [] [] input)
{
List< Pair> result = new ArrayList< Pair>();
for(int i = 0; i {
result.add(new Pair(input [i] [0] input [i] [1]));
}
返回结果;
}
public static WritableWorkbook createExcel(File excelFile,Map< String,List&Pair>> worksheets)throws IOException,WriteException
{
WritableWorkbook result = .createWorkbook(excelFile);
int Order = 0;
for(String worksheetName:worksheets.keySet())
{
WritableSheet worksheet = result.createSheet(worksheetName,order ++);
List< Pair> worksheetValues = worksheets.get(worksheetName);
for(int row = 0; row< worksheetValues.size(); ++ row)
{
worksheet.addCell(new jxl.write.Number(0,row,worksheetValues。 get(row).getX()));
worksheet.addCell(new jxl.write.Number(1,row,worksheetValues.get(row).getY()));
}
}
返回结果;
}
}
类对
{
private double x;
private double y;
对(double x,double y)
{
this.x = x;
this.y = y;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
}
I have a hashMap in java in terms of some keys which each key is indicating a flow. Then each value showing statics about each packet belongs to that flow.
What I need to do is, to draw graphs for each flow based on those values. for example:
Flow1: {[length, time],[],[],...}
Flow2: {[length, time],[length, time],[],...}
i need to create a CSV file that then can be read from MS excel. Can anyone has the idea to give me some clues please?
Edited: here is my hashMap:
Iterator<Flows> iterator = myHashMap.keySet().iterator();
String fileName = ((args.length > 0) ? args[0] : "jexcel.xls");
Map<String, ArrayList> csv = new HashMap<String, ArrayList>();
int i=0;
while(iterator.hasNext()){
Flows key = iterator.next();
ArrayList value = myHashMap.get(key);
csv.put("Flow["+i+"]", value);
}
If you really want an Excel file, the best library for creating one is Andy Khan's JExcel.
I think you'd need one worksheet per flow, with .csv pairs for each one, sorted by time.
If these are graphs of a variable versus time, wouldn't "time" be the first value in each pair?
Here's how I'd do it. It works perfectly for the simple test case I supplied - it's working code that you'll be able to extend.
package jexcel;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JExcelUtils
{
public static void main(String[] args)
{
String fileName = ((args.length > 0) ? args[0] : "jexcel.xls");
Map<String, List<Pair>> csv = new HashMap<String, List<Pair>>()
{{
put("Flow1", fromArrayToList(new double[][]
{
{ 0.0, 0.0 },
{ 0.1, 1.0 },
{ 0.2, 2.0 },
{ 0.3, 3.0 },
{ 0.4, 4.0 },
{ 0.5, 5.0 },
}));
put("Flow2", fromArrayToList(new double[][]
{
{ 0.0, 0.0 },
{ 0.1, 1.0 },
{ 0.2, 4.0 },
{ 0.3, 9.0 },
{ 0.4, 16.0 },
{ 0.5, 25.0 },
}));
}};
WritableWorkbook excelContents = null;
try
{
File excelFile = new File(fileName);
excelContents = createExcel(excelFile, csv);
excelContents.write();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (WriteException e)
{
e.printStackTrace();
}
finally
{
try { if (excelContents != null) excelContents.close(); } catch (Exception e) { e.printStackTrace(); }
}
}
public static List<Pair> fromArrayToList(double [][] input)
{
List<Pair> result = new ArrayList<Pair>();
for (int i = 0; i < input.length; ++i)
{
result.add(new Pair(input[i][0], input[i][1]));
}
return result;
}
public static WritableWorkbook createExcel(File excelFile, Map<String, List<Pair>> worksheets) throws IOException, WriteException
{
WritableWorkbook result = Workbook.createWorkbook(excelFile);
int order = 0;
for (String worksheetName : worksheets.keySet())
{
WritableSheet worksheet = result.createSheet(worksheetName, order++);
List<Pair> worksheetValues = worksheets.get(worksheetName);
for (int row = 0; row < worksheetValues.size(); ++row)
{
worksheet.addCell(new jxl.write.Number(0, row, worksheetValues.get(row).getX()));
worksheet.addCell(new jxl.write.Number(1, row, worksheetValues.get(row).getY()));
}
}
return result;
}
}
class Pair
{
private double x;
private double y;
Pair(double x, double y)
{
this.x = x;
this.y = y;
}
public double getX()
{
return x;
}
public double getY()
{
return y;
}
}
这篇关于从HashMap中以java创建CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!