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();
}
}
}