XmlGrid Users Forum
2013-07-08 08:28:50
/*
Example -- Create an Excel file with two sheets
*/

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import jxl.write.WritableWorkbook;

/**
 * @author Zac Zeng
 *
 */
public class ExcelWriter {

/**
* @param args
* @throws IOException 
*/
public HSSFWorkbook createHSSFWorkbook()
{
HSSFWorkbook workbook = new HSSFWorkbook();
return workbook;
}
public void write(String fn,HSSFWorkbook bk) throws IOException
{
OutputStream output = new FileOutputStream(fn);
bk.write(output);
output.close();
}
public void addColumn(HSSFSheet sht, Vector<Integer> v)
{
int i,j,n,nCurrentRows;
int val;
HSSFRow row;
n=v.size();
nCurrentRows=sht.getLastRowNum();
j=0;
for(i=0;i<n;i++)
{
val=v.get(i);
if(i<nCurrentRows)
{
row=sht.getRow(i);
}
else
{
row= sht.createRow(i);
}
HSSFCell cell = row.createCell(j);
cell.setCellValue(val);
}
}
public void addCell(HSSFRow row, String val,int j)
{
HSSFCell cell = row.createCell(j);
cell.setCellValue(val);
}
public void addCell(HSSFRow row, int val,int j)
{
HSSFCell cell = row.createCell(j);
cell.setCellValue(val);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String fn;
fn= "test.xls";
WritableWorkbook bk;
ExcelWriter ew=new ExcelWriter();
HSSFWorkbook hbk=ew.createHSSFWorkbook();
Vector<Integer> v1=new Vector<Integer>();
v1.add(111);
v1.add(112);
HSSFSheet sheet1 = hbk.createSheet("Sample sheet 1 ");
ew.addColumn(sheet1,v1);

v1.add(113);
v1.add(115);
HSSFSheet sheet2 = hbk.createSheet("Sample sheet 2");
ew.addColumn(sheet2,v1);
ew.write(fn, hbk);
}
}

;3501;2013-07-08 08:28:50__s__
/*
Example -- Create an Excel file with two sheets
*/

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import jxl.write.WritableWorkbook;

/**
 * @author Zac Zeng
 *
 */
public class ExcelWriter {

/**
* @param args
* @throws IOException 
*/
public HSSFWorkbook createHSSFWorkbook()
{
HSSFWorkbook workbook = new HSSFWorkbook();
return workbook;
}
public void write(String fn,HSSFWorkbook bk) throws IOException
{
OutputStream output = new FileOutputStream(fn);
bk.write(output);
output.close();
}
public void addColumn(HSSFSheet sht, Vector<Integer> v)
{
int i,j,n,nCurrentRows;
int val;
HSSFRow row;
n=v.size();
nCurrentRows=sht.getLastRowNum();
j=0;
for(i=0;i<n;i++)
{
val=v.get(i);
if(i<nCurrentRows)
{
row=sht.getRow(i);
}
else
{
row= sht.createRow(i);
}
HSSFCell cell = row.createCell(j);
cell.setCellValue(val);
}
}
public void addCell(HSSFRow row, String val,int j)
{
HSSFCell cell = row.createCell(j);
cell.setCellValue(val);
}
public void addCell(HSSFRow row, int val,int j)
{
HSSFCell cell = row.createCell(j);
cell.setCellValue(val);
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String fn;
fn= "test.xls";
WritableWorkbook bk;
ExcelWriter ew=new ExcelWriter();
HSSFWorkbook hbk=ew.createHSSFWorkbook();
Vector<Integer> v1=new Vector<Integer>();
v1.add(111);
v1.add(112);
HSSFSheet sheet1 = hbk.createSheet("Sample sheet 1 ");
ew.addColumn(sheet1,v1);

v1.add(113);
v1.add(115);
HSSFSheet sheet2 = hbk.createSheet("Sample sheet 2");
ew.addColumn(sheet2,v1);
ew.write(fn, hbk);
}
}

__s__~u=Zac Zeng~site=NULL__s__Create mutiple sheet Excel file in JAVA example__s____m__

Best view with Google Chrome