Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

POI anyone?Open a Excel file, grab data, open/save in a *.txt

Status
Not open for further replies.

Kruzer

Programmer
Jun 16, 2000
117
US
I have the workbook
fs = new POIFSFileSystem(new FileInputStream("c:\\POI\\test.xls"));

I parse through the data and I would like to output the data in a desired format (*.txt). What Class do I use in the POI API to perform such an action?

I can output the data without a problem to excel
fileOut = new FileOutputStream("c:\\POI\\testFile.xls");

Is it just a matter of adjusting the file mime type, if so, to what?

Thanks!

Dano
dan_kryzer@hotmail.com
What's your major malfunction
 
I don't think there's a POI api that just outputs the spreadsheet content in another format.

However, you can read the spreadsheet a cell at a time into Strings and output the results to a txt file yourself.
 
I have code started setting cells in a new excel file to just test the output and when I add System.out.println statements things look great. How do I output to a txt file myself?

FileOutputStream, what method or technique should I use?


for(int z = 0; z < sheet.getLastRowNum()+1; z++ ) {
row = sheet.getRow(z);

for(int i=0; i < 8; i++) //sheet.getLastRowNum()
{
cell = row.getCell((short)i);

if (z == 0)
{
hstbl.put(&quot;&quot;+i+&quot;&quot; , cell.getStringCellValue());
temp = (String) hstbl.get(&quot;&quot;+i+&quot;&quot;);

cell = row.createCell((short)i);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(temp);
}

... and so on ...

FileOutputStream fileOut = null;

// something down here ?

try {
fileOut = new FileOutputStream(&quot;c:\\POI\\testFile.xls&quot;);
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}


Dano
dan_kryzer@hotmail.com
What's your major malfunction
 
I figured it out, thanks.

FileOutputStream out;
PrintStream p;

try
{
out = new FileOutputStream(&quot;C:\\test.txt&quot;);
p = new PrintStream( out );
p.println (&quot;This is written to a file&quot;);
p.close();
}
catch (Exception e)
{
System.err.println (&quot;Error writing to file&quot;);
}

Dano
dan_kryzer@hotmail.com
What's your major malfunction
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top