java excel jxl 导出_Java导出成Excel的方法(需要jxl.jar包)

import java.io.File;

import java.sql.ResultSet;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import org.eclipse.swt.SWT;

import org.eclipse.swt.widgets.FileDialog;

import org.eclipse.swt.widgets.Shell;

public class Export2Excel {

public Export2Excel(Shell shell, String[] coloumNames, ResultSet rSet,

String sheetName) {

FileDialog fileDialog = new FileDialog(shell, SWT.NONE);

fileDialog.setFilterExtensions(new String[]{"*.xls"});

fileDialog.setText("文件另存为");

fileDialog.setFileName("temp.xls");

fileDialog.open();

String path = fileDialog.getFilterPath();

String fileName = fileDialog.getFileName();

try {

WritableWorkbook book = Workbook.createWorkbook((new File(path

+ "\\" + fileName)));

WritableSheet sheet = book.createSheet(sheetName, 0);

for (int i = 0; i < coloumNames.length; i++) {

sheet.setColumnView(100, 400);

Label label = new Label(i, 0, coloumNames[i]);

sheet.addCell(label);

}

int i = 0;

while (rSet.next()) {

i = i + 1;

Label label1A = new Label(0, i, rSet.getRow() + "");

sheet.addCell(label1A);

for (int j = 0; j < coloumNames.length; j++) {

System.out.println("j =" + j);

if (j == 0) {

Label label = new Label(j, i, rSet.getRow() + "");

sheet.addCell(label);

} else {

Label label = new Label(j, i, rSet.getString(j));

sheet.addCell(label);

}

}

}

book.write();

book.close();

} catch (Exception ex) {

ex.printStackTrace();

}

}

}

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2012-05-17 10:10

浏览 1467

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值