export data to file using java
simple java programe to export to filewriter
how to export to filewriter using java source code
/**
* @author JohnJustin
www.johnjustin.tk
*
*/
public class ExportFileWriter{
int intPageNumber = 0;
java.sql.ResultSet rsExportResult = null;
ResultSetBuilder resultBuilder = ResultSetBuilderFactory.getInstance();
POIExcelWriter poiExcelWriter = null;
TextWriter textWriter = null;
ExportBuilder exportBuilder = null;
private int intRowIndex = 0;
public ExportFileWriter(ExportBuilder exportBuilder, TextWriter textWriter){
this.textWriter = textWriter;
this.exportBuilder = exportBuilder;
}
public ExportFileWriter(ExportBuilder exportBuilder, POIExcelWriter poiExcelWriter){
this.exportBuilder = exportBuilder;
this.poiExcelWriter = poiExcelWriter;
}
/*
* @Method : setTextWriter
* @param1 : TextWriter
* sets the text writer
*/
public void setTextWriter(TextWriter textWriter){
this.textWriter = textWriter;
}
/*
* @Method : setPOIExcelWriter
* @param1 : POIExcelWriter poiExcelWriter
* sets the poi excel writer
*/
public void setPOIExcelWriter(POIExcelWriter poiExcelWriter){
this.poiExcelWriter = poiExcelWriter;
}
/*
* @Method : setExportBuilder
* @param1 : ExportBuilder exportBuilder
* sets the ExportBuilder instance
*/
public void setExportBuilder(ExportBuilder exportBuilder){
this.exportBuilder = exportBuilder;
}
/*
* @Method : writeToTextFile
* @param : no param
* write the contents to the text file.
*/
public boolean writeToTextFile(){
java.io.BufferedWriter bufferedWriter = new java.io.BufferedWriter(textWriter);
try{
int numberOfColumns = 0;
/*if paging is enabled, we are getting the current page and querying the page counted.*/
if(exportBuilder.isPagingEnabled()){
int intCurrentPage = resultBuilder.getCurrentPage();
if(intCurrentPage != -1) rsExportResult = resultBuilder.getResultSet(rsExportResult, intCurrentPage);
}else{
rsExportResult = resultBuilder.getResultSet(rsExportResult);
}
if(rsExportResult != null){
/*determining the fields to be printed as header and getting the number of columns and printing the header.*/
String strHeader[] = null;
if(exportBuilder.getHeader() != null) strHeader = exportBuilder.getHeader();
if(strHeader == null && exportBuilder.getExportFields() != null) strHeader = exportBuilder.getExportFields();
if(exportBuilder.getExportFields() == null || exportBuilder.getFieldPosition() == null){
java.sql.ResultSetMetaData rsMetaData = rsExportResult.getMetaData();
numberOfColumns = rsMetaData.getColumnCount();
if(strHeader == null){
strHeader = new String[numberOfColumns];
for(int index=0; index<numberOfColumns; index++){
strHeader[index] = rsMetaData.getColumnName(index + 1);
}
}
}
if(exportBuilder.getExportFields() != null){
numberOfColumns = exportBuilder.getExportFields().length;
}else if(exportBuilder.getFieldPosition() != null){
numberOfColumns = exportBuilder.getFieldPosition().length;
}
for(int index=0; index<strHeader.length; index++){
if(index == 0){
bufferedWriter.write(strHeader[index]);
}else{
bufferedWriter.write(exportBuilder.getDelimiter() + strHeader[index]);
}
}
bufferedWriter.write("\r\n");
strHeader = null;
/*end of determining the fields to be printed as header and getting the number of columns and printing the header*/
/*writing the header */
if(exportBuilder.isPagingEnabled()){
int intCurrentPage = 0;
do{
writeResultSetToTextFile(rsExportResult, bufferedWriter, numberOfColumns);
if(rsExportResult != null) rsExportResult.close();
intCurrentPage = resultBuilder.getCurrentPage();
if(intCurrentPage == -1) break;
rsExportResult = resultBuilder.getResultSet(rsExportResult, intCurrentPage);
}while(rsExportResult != null);
}else{
writeResultSetToTextFile(rsExportResult, bufferedWriter, numberOfColumns);
if(rsExportResult != null) rsExportResult.close();
}
}
}catch(Exception e){
System.out.println("Error in writeToTextFile() ------ " + e);
}
try{
if(bufferedWriter != null) bufferedWriter.close();
}catch(Exception ee){
}
return(true);
}
public boolean writeToExcelFile(){
try{
int numberOfColumns = 0;
/*if paging is enabled, we are getting the current page and querying the page counted.*/
if(exportBuilder.isPagingEnabled()){
int intCurrentPage = resultBuilder.getCurrentPage();
if(intCurrentPage != -1) rsExportResult = resultBuilder.getResultSet(rsExportResult, intCurrentPage);
}else{
rsExportResult = resultBuilder.getResultSet(rsExportResult);
}
if(rsExportResult != null){
poiExcelWriter.resetSheetIndex();
poiExcelWriter.setSheet();
/*determining the fields to be printed as header and getting the number of columns and printing the header.*/
String[] strHeader = null;
if(exportBuilder.getHeader() != null) strHeader = exportBuilder.getHeader();
if(strHeader == null && exportBuilder.getExportFields() != null) strHeader = exportBuilder.getExportFields();
if(exportBuilder.getExportFields() == null || exportBuilder.getFieldPosition() == null){
java.sql.ResultSetMetaData rsMetaData = rsExportResult.getMetaData();
numberOfColumns = rsMetaData.getColumnCount();
if(strHeader == null){
strHeader = new String[numberOfColumns];
for(int index=0; index<numberOfColumns; index++){
strHeader[index] = rsMetaData.getColumnName(index + 1);
}
}
}
if(exportBuilder.getExportFields() != null){
numberOfColumns = exportBuilder.getExportFields().length;
}else if(exportBuilder.getFieldPosition() != null){
numberOfColumns = exportBuilder.getFieldPosition().length;
}
poiExcelWriter.insertRow(intRowIndex);
for(int index=0; index<strHeader.length; index++){
poiExcelWriter.writeData(index, intRowIndex, strHeader[index]);
}
strHeader = null;
intRowIndex++;
/*end of determining the fields to be printed as header and getting the number of columns and printing the header*/
/*writing the header */
/*writing the data*/
if(exportBuilder.isPagingEnabled()){
int intCurrentPage = 0;
do{
writeResultSetToExcelFile(rsExportResult, numberOfColumns);
if(rsExportResult != null) rsExportResult.close();
intCurrentPage = resultBuilder.getCurrentPage();
if(intCurrentPage == -1) break;
rsExportResult = resultBuilder.getResultSet(rsExportResult, intCurrentPage);
}while(rsExportResult != null);
}else{
writeResultSetToExcelFile(rsExportResult, numberOfColumns);
if(rsExportResult != null) rsExportResult.close();
}
}
}catch(Exception e){
System.out.println("Error in writeToExcelFile() ------ " + e);
}
try{
if(poiExcelWriter != null) poiExcelWriter.closeExcel();
}catch(Exception ee){
}
return(true);
}
private void writeResultSetToTextFile(java.sql.ResultSet rsExportResult, java.io.BufferedWriter bufferedWriter, int numberOfColumns){
try{
while(rsExportResult.next()){
if(numberOfColumns != 0){
for(int intFieldPostion = 0; intFieldPostion < numberOfColumns; intFieldPostion++){
if(exportBuilder.getExportFields() != null){
if(intFieldPostion == 0){
bufferedWriter.write(rsExportResult.getString(exportBuilder.getExportFields()[intFieldPostion]));
}else{
bufferedWriter.write(exportBuilder.getDelimiter() + rsExportResult.getString(exportBuilder.getExportFields()[intFieldPostion]));
}
}else if(exportBuilder.getFieldPosition() != null){
if(intFieldPostion == 0){
bufferedWriter.write(rsExportResult.getString(exportBuilder.getFieldPosition()[intFieldPostion]));
}else{
bufferedWriter.write(exportBuilder.getDelimiter() + rsExportResult.getString(exportBuilder.getFieldPosition()[intFieldPostion]));
}
}else{
if(intFieldPostion == 0){
bufferedWriter.write(rsExportResult.getString(intFieldPostion + 1));
}else{
bufferedWriter.write(exportBuilder.getDelimiter() + rsExportResult.getString(intFieldPostion + 1));
}
}
}
bufferedWriter.write("\r\n");
}
}
}catch(Exception e){
System.out.println("Error in writeResultSetToTextFile() --------- " + e);
}
}
private void writeResultSetToExcelFile(java.sql.ResultSet rsExportResult, int numberOfColumns){
try{
while(rsExportResult.next()){
if(intRowIndex != 0 && (intRowIndex % 65536) == 0){
poiExcelWriter.setSheet();
intRowIndex = 0;
}
poiExcelWriter.insertRow(intRowIndex);
if(numberOfColumns != 0){
for(int intFieldPostion = 0; intFieldPostion < numberOfColumns; intFieldPostion++){
if(exportBuilder.getExportFields() != null){
poiExcelWriter.writeData(intFieldPostion, intRowIndex, rsExportResult.getString(exportBuilder.getExportFields()[intFieldPostion]));
}else if(exportBuilder.getFieldPosition() != null){
poiExcelWriter.writeData(intFieldPostion, intRowIndex, rsExportResult.getString(exportBuilder.getFieldPosition()[intFieldPostion]));
}else{
poiExcelWriter.writeData(intFieldPostion, intRowIndex, rsExportResult.getString(intFieldPostion + 1));
}
}
intRowIndex++;
}
}
}catch(Exception e){
System.out.println("Error in writeResultSetToExcelFile() --------- " + e);
}
}
}
1 comments:
do i need to insert any jar file because it is showing me may errors at the ResultSetBuilder and many more
Post a Comment