I suggest to use Apache POI for operations with .xls
and .xlsx
files. Here you can find some tutorial suggestions: Learning Apache POI for Java
storing the output of java program in an excel sheet through Apachae POI
-
06-08-2022 - |
Question
I have the below csv file which is comma delimited , now from this file i already have read the value of the column der_id ,but please advise now the output that i am getting on console that is the column der_id and its value I want to be stored it in a separate excel sheet in the first column itself. please advise how can I achieve this through 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
presently I am reading this in this format..
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");
}
}
and its output is in this format on console i am getting..
der_id
694698683
694698686
No correct solution
OTHER TIPS
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();
}
this is a sample code which writes dummy data in to excel file you can use it according to your data
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow