Solved

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

Posted on 2014-12-21
17
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 125
ejb entity bean example issue 2 25
DTD and JAVA versions 1 29
IBM TS2900 (3572) Tape Autoloader Java? 12 59
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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
This video teaches viewers about errors in exception handling.

756 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