Solved

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

Posted on 2014-12-21
17
153 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 31

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 31

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 31

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 31

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 31

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Foolproof security solutions has become one of the key necessities of every e-commerce or Internet banking website. If you too own an online shopping site then its vital for you to equip your web portal with customer security features that can allow…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
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…
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now