Solved

how to create the textfiles based on excel sheet content using java

Posted on 2014-12-21
17
157 Views
Last Modified: 2014-12-27
hi

i have one excel sheet it contains two columns
one column contains exachange id
another column contains requested xml

from this excel sheet i have to create txt files
by using exchange id i have to create txtfilename
this file containss second column request xml data

how many exchangeids are there in excel sheet those many txt files i have to create
and insert request xmls into it.

can any one suggest how to do it using java

Thanks
0
Comment
Question by:srikotesh
  • 8
  • 5
  • 3
17 Comments
 
LVL 61

Expert Comment

by:gheist
ID: 40512462
Excel sheet can serve as a java data source via ODBC-JDBC bridge.
0
 
LVL 1

Author Comment

by:srikotesh
ID: 40513540
i know how to read the data from excel sheet using java program
after that i have create txt file and i have to add xml into it.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40513555
Can you show a few sample rows from your spreadsheet and, using those rows, create the text file you would like as a result?
0
 
LVL 32

Expert Comment

by:awking00
ID: 40513640
Really do need to see a sample of the workbook. Is there only one worksheet? Do you want a text file for each xml value in the spreadsheet? Are there headers in the worksheet? Is there any modification to the xml required? Are you looking to create files with .txt extensions or with .xml extensions? I do a lot of this stuff (mostly in the other direction - xml to xls, though) and just need to get a clearer picture of what you need.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40514840
Do you still need help with this?
0
 
LVL 1

Author Comment

by:srikotesh
ID: 40515305
HI awking00,

Sorry for the delay,
yes i need help please see the attached excel sheet.
REQUEST-XML.xlsx
0
 
LVL 1

Author Comment

by:srikotesh
ID: 40515316
please see my answers for your questions

Do you want a text file for each xml value in the spreadsheet?
yes i need a txt file for each xml

Are you looking to create files with .txt extensions or with .xml extensions?
i am looking for .txt extension
0
 
LVL 32

Accepted Solution

by:
awking00 earned 400 total points
ID: 40516620
Try this -
package gov.irs.xrdb.xml2txt;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

public class XMLFileConverter {

	// The path for your txt files. Note the slash needs to be escaped
	public static String path = "C:\\Users\\ZW5FB\\Documents\\EE_files\\";
	
	public static void main(String[] args) throws IOException, InvalidFormatException {

		// The argument to pass in is the full path and filename for your Excel file
		String xlsFilename = args[0];
		FileInputStream fis = new FileInputStream(new File(xlsFilename));		
		Workbook wb = WorkbookFactory.create(fis);
		Sheet sheet = wb.getSheetAt(0);
		Iterator<Row> rows = sheet.rowIterator();
		while (rows.hasNext()) {
			Row row = rows.next();
			int rownum = row.getRowNum();
			if (rownum == 0) {
				continue;
			}
			String id = row.getCell(0).getStringCellValue();
			String xml = row.getCell(1).getStringCellValue().trim();
			writeFile(id, xml);
		}
	}

	public static void writeFile(String id, String xml) throws IOException {
		String txtFilename = id + ".txt";
		File file = new File(path + txtFilename);
		FileWriter fw = new FileWriter(file);
		PrintWriter pw = new PrintWriter(fw);
		String[] lines = xml.split("\n");
		for (String line : lines) {
			pw.println(line);
		}
		pw.close();
		fw.close();
	}
}

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:srikotesh
ID: 40517468
while running this program i am getting the below exception
i tried with .xls and .xlsx formats but still i am getting this error

java.lang.NoClassDefFoundError: InvalidFormatException
Caused by: java.lang.ClassNotFoundException: InvalidFormatException
0
 
LVL 1

Author Comment

by:srikotesh
ID: 40517513
above issue is resolved now i am gettting
java.lang.ArrayIndexOutOfBoundsException: 0
      at com.org.XMLFileConverter.main(XMLFileConverter.java:25)
0
 
LVL 61

Assisted Solution

by:gheist
gheist earned 100 total points
ID: 40517949
InvalidFormatException means that you compiled .class with more recent javac than you are using to run it.
0
 
LVL 1

Author Comment

by:srikotesh
ID: 40518826
I've requested that this question be closed as follows:

Accepted answer: 0 points for srikotesh's comment #a40517468

for the following reason:

I got the proper solution what i am looking.Thx
0
 
LVL 61

Expert Comment

by:gheist
ID: 40518827
Could you point us to the answer that is the "proper solution", or detail different approach you took?
0
 
LVL 32

Expert Comment

by:awking00
ID: 40519645
>>I got the proper solution what i am looking.<<
Can you please post the "proper solution"?
0
 
LVL 1

Author Comment

by:srikotesh
ID: 40519706
hi awking00,

whatever u gave the code that works for me
after removing the line
String xlsFilename = args[0]; from the above code.
0
 
LVL 1

Author Closing Comment

by:srikotesh
ID: 40519707
Thanks for u suggestions
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 80
bitbucket vs gitbucket 3 55
Java DateChooser? 3 29
Java Loop 6 48
Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now