于java导出成Excel的方法(需要jxl.jar包)2014-10-20 16:41:09

( 还没有投票,继续加油! )
分享:
31.3K

需提前导入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();
    }
  }
}






头像

snowcoal
  • java
  • 导出excel

本文标签:

java导出excel

收藏到我的私密空间

标题:于java导出成Excel的方法(需要jxl.jar包)

作者:柳岸花明

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限

如果本站的内容有幸帮助到了您,建议您了解一下当页的广告内容哦,我们的进步离不开您的支持,Thank you~