How to combine two excel files using java -
i having 3 xls file. need create new file combining 3 files.
the files follows:
- file 1 has 2 sheets
- file 2 has 1 sheet
- file 3 has 1 sheet
the goal have sheets of 3 files in 1 single file.
here have tried:
// converting csv excel: dateformat dateformat = new simpledateformat("mmddyyyy-hhmmss"); date date = new date(); string date1 = dateformat.format(date); workbook wb = new hssfworkbook(); creationhelper helper = wb.getcreationhelper(); sheet sheet = wb.createsheet("ebs_input_file"); string path = "path.csv"; csvreader reader = new csvreader(new filereader(path)); string[] line; string web; set<string> data = new hashset<string>(); int r = 0; stringtokenizer st = null; string strline = ""; int token = 0; string address = null; while ((line = reader.readnext()) != null) { row row = sheet.createrow((short) r); (int = 0; < line.length; i++) row.createcell(i).setcellvalue(helper.createrichtextstring(line[i])); st = new stringtokenizer(strline, ","); while (st.hasmoretokens()) { token++; string token1 = st.nexttoken(); } r++; fileoutputstream fileout = new fileoutputstream("excel.xls"); wb.write(fileout); fileout.close(); } system.out.println("file converted successful!"); // excel file 1 workbook sourcebook1 = new hssfworkbook( new fileinputstream("excel2.xls")); // excel file 2 workbook sourcebook2 = new hssfworkbook( new fileinputstream("excel.xls")); // combining excel file 1 & 2 sourcebook1.combine(sourcebook2); // saving in excel file 3 sourcebook1.save("excel3.xls"); `
also tried:
string [] excelsheets = {"excel.xls","excel2.xls"}; //root folder excel files read final string root_folder = "path"; //output file name merged excel file (consolidated file) final string output_file = path+"excel3.xls"; workbook [] workbook = new workbook[excelsheets.length]; for(int = 0; < excelsheets.length; i++) { workbook[i] = new hssfworkbook(new fileinputstream(root_folder + excelsheets[i])); } workbook newworkbook = new hssfworkbook(); for(int = 0; < excelsheets.length; i++) { newworkbook = copyexcelsheets.copy(workbook[i], newworkbook, "book-" + i); } //write on new file fileoutputstream fileout1; fileout1 = new fileoutputstream(output_file); newworkbook.write(fileout1); for(int = 0; < excelsheets.length; i++) { workbook[i].close(); } newworkbook.close(); fileout1.close(); }
but can't achieve desired result. doing wrong?
Comments
Post a Comment