poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Knepp, Ethan" <eKn...@c-cor.net>
Subject RE: Can POI create a CSV file?
Date Thu, 03 Jul 2003 19:40:41 GMT
Thanks!

I will take a look at it and give it run.

-Ethan

-----Original Message-----
From: Nate [mailto:nmcminn@charter.net]
Sent: Thursday, July 03, 2003 3:43 PM
To: POI Users List
Subject: Re: Can POI create a CSV file?


I've got a ready made class that takes a resultset and returns a CSV, here's
the source, should work (disclaimer, may not ;-)...

package com.whatever.yourpackage;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.ResultSetMetaData;

public class CSVBuilder {

  public CSVBuilder() {}

  public String buildCSV(ResultSet rs) throws Exception {

    StringBuffer sb = new StringBuffer("");
    ResultSetMetaData meta;
    if(rs == null) {
      return "";
    }

    try {

      //get meta data
      meta = rs.getMetaData();

      //create header row
      for(int i = 0; i < meta.getColumnCount(); i++) {
        if(i == meta.getColumnCount() - 1) {
          sb.append(meta.getColumnName(i));
        }else {
          sb.append(meta.getColumnName(i) + ",");
        }
      }

      sb.append("\r\n");

      //build a row for each entry
      while(rs.next()) {
        for(int i = 0; i < meta.getColumnCount(); i++) {
          if(i == meta.getColumnCount() - 1) {
            sb.append(rs.getString(i));
          }else {
            sb.append(rs.getString(i) + ",");
          }
        }
        sb.append("\r\n");
      }
    }
    catch(SQLException err) {throw err;}

    return sb.toString();
  }
}

----- Original Message -----
From: "Knepp, Ethan" <eKnepp@c-cor.net>
To: "'POI Users List'" <poi-user@jakarta.apache.org>
Sent: Thursday, July 03, 2003 2:26 PM
Subject: RE: Can POI create a CSV file?


> Ya, I didn't know if there was a utility api that would take a WorkBook
and
> its worksheets and create multiple csv files.  Just thought I would ask.
>
> I will just resort to a solution similart to your suggestion Nate
>
> Thanks!
>
>
> -----Original Message-----
> From: Nate [mailto:nmcminn@charter.net]
> Sent: Thursday, July 03, 2003 3:27 PM
> To: POI Users List
> Subject: Re: Can POI create a CSV file?
>
>
> I don't think that POI can do this, but it begs the question: Why bother
> with the extra overhead?  Just build a StringBuffer and append a row to it
> for each row in the resultset.  Very fast, very easy.
>
> --Nathan McMinn
>
> ----- Original Message -----
> From: "Knepp, Ethan" <eKnepp@c-cor.net>
> To: "POI User List (E-mail)" <poi-user@jakarta.apache.org>
> Sent: Thursday, July 03, 2003 2:17 PM
> Subject: Can POI create a CSV file?
>
>
> > I currently am using the POI's HSSF to write the entire results from an
> SQL
> > queries Result Set into a Excel Spreadsheet.  This was easy and it works
> > great.
> >
> > I recently got a request from a customer to modify the utility to allow
> for
> > the result set to be written to a Comma Separated Value(CSV) file.  Does
> POI
> > have any utility methods/APIs to write to CSV files?
> >
> > Thanks,
> > Ethan
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: poi-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-user-help@jakarta.apache.org

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message