Solved

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

Posted on 2014-12-21
17
160 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 62

Expert Comment

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

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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 2

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 2

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
 
LVL 2

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 2

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 62

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 2

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 62

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 2

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 2

Author Closing Comment

by:srikotesh
ID: 40519707
Thanks for u suggestions
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Edit a page at wix.com 8 69
hibernate insert example 13 34
CSV file parsing thru Java 13 34
Is Applet the way to go for my drag and drop system? 8 25
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

827 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