我有以下csv文件,这是逗号分隔,现在从这个文件我已经读取了列der_id的值,但请现在告诉我在控制台上获得的输出是列der_id及其值I希望将其存储在第一列本身的单独Excel工作表中。请告知我如何通过apache poi实现这一目标
wert,der_tran,der_id,der_version,cvns_num,cvs_type
AB42126325,0,694698683,0,651626843,13002
AB42126326,0,694698686,0,651626846,13001
目前我正在以这种格式阅读..
public class Parsingcsv {
public static void main(String[] args)
{
Parsingcsv obj = new Parsingcsv();
obj.run();
}
public void run()
{
String csvFile = "C:\\abc_2.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
try
{
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
// use comma as separator
String[] id = line.split(cvsSplitBy);
System.out.println("[der_id= " + id[2] + "]");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("#####################3");
}
}
并且它的输出在控制台上以这种格式得到..
der_id
694698683
694698686
答案 0 :(得分:0)
我建议使用Apache POI进行.xls
和.xlsx
文件的操作。在这里,您可以找到一些教程建议:Learning Apache POI for Java
答案 1 :(得分:0)
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sample sheet");
Map<String, Object[]> data = new HashMap<String, Object[]>();
data.put("1", new Object[] {"Emp No.", "Name", "Salary"});
data.put("2", new Object[] {1d, "John", 1500000d});
data.put("3", new Object[] {2d, "Sam", 800000d});
data.put("4", new Object[] {3d, "Dean", 700000d});
Set<String> keyset = data.keySet();
int rownum = 0;
for (String key : keyset) {
Row row = sheet.createRow(rownum++);
Object [] objArr = data.get(key);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
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);
}
}
try {
FileOutputStream out =
new FileOutputStream(new File("C:\\new.xls"));
workbook.write(out);
out.close();
System.out.println("Excel written successfully..");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这是一个示例代码,它将虚拟数据写入excel文件,您可以根据您的数据使用它