Solved

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

Posted on 2014-12-21
17
165 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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

726 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