应用jxl框架进行Excel文件解析

1、在项目开发过程中需要将Excel文件导入到数据库中,应用到jxl.jar 进行解析

2、如下

  1. package test;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.io.UnsupportedEncodingException;
  5. import javax.servlet.http.HttpServletResponse;
  6. import jxl.Sheet;
  7. import jxl.Workbook;
  8. import jxl.WorkbookSettings;
  9. import jxl.read.biff.BiffException;
  10. import jxl.write.Label;
  11. import jxl.write.WritableSheet;
  12. import jxl.write.WritableWorkbook;
  13. import jxl.write.WriteException;
  14. public class JxlTest {
  15.     public static void main(String[] args) {
  16.         (new JxlTest()).excelRead();
  17.     }
  18.     public void excelRead(){
  19.         File file = new File("d://cicc825.xls");
  20.         try {
  21.             WorkbookSettings workbookSettings=new WorkbookSettings();
  22.             workbookSettings.setEncoding("ISO-8859-1"); //关键代码,解决中文乱码
  23.             Workbook book= Workbook.getWorkbook(file,workbookSettings);
  24.             //Workbook book= Workbook.getWorkbook(file);
  25.             Sheet sheet=book.getSheet(0); 
  26.             int row = sheet.getRows();
  27.             int column =sheet.getColumns();
  28.             for(int i=0;i<row;i++){
  29.                 for(int j=0;j<column;j++){
  30.                     System.out.println(sheet.getCell(j, i).getContents());
  31.                 }
  32.             }
  33.         } catch (BiffException e) {
  34.             e.printStackTrace();
  35.         } catch (IOException e) {
  36.             e.printStackTrace();
  37.         }
  38.     }
  39.     
  40.     public void excelWrite(HttpServletResponse response) {
  41.         long time = System.currentTimeMillis();// 用当前的系统时间作为默认表名
  42.         String name = String.valueOf(time);
  43.         name ="position";
  44.         String pathName = name + ".xls";
  45.         try {
  46.             pathName = new String(pathName.getBytes(), "ISO-8859-1");
  47.         } catch (UnsupportedEncodingException e) {
  48.             e.printStackTrace();
  49.         }
  50.         // 获取弹出保存框的保存路径和表名
  51.         response.setContentType("APPLICATION/OCTET-STREAM");
  52.         response.setHeader("Content-Disposition""attachment; filename=/"" + pathName + "/"");
  53.         
  54.         WritableWorkbook workbook;
  55.         WorkbookSettings   workbookSettings=new   WorkbookSettings(); 
  56.         workbookSettings.setEncoding("GB2312");
  57.         try {
  58.             workbook = Workbook.createWorkbook(response.getOutputStream(),workbookSettings);
  59.             WritableSheet ws = workbook.createSheet("sheet 1"0);
  60.             ws.setColumnView(0,22);
  61.             Label cell = null;
  62.             for(int i=0;i<2;i++){
  63.                 cell = new Label(0, i+1"id");
  64.                 ws.addCell(cell);
  65.                 cell = new Label(1, i+1"name");
  66.                 ws.addCell(cell);
  67.                 
  68.             }
  69.             workbook.write();
  70.             workbook.close();   // 一定要关闭, 否则没有保存Excel;
  71.         } catch (IOException e) {
  72.             e.printStackTrace();
  73.         }catch (WriteException e) {
  74.             e.printStackTrace();
  75.         } 
  76.         
  77.     }
  78.     
  79. }

3、解析过程还是比较简单的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值