Excel快速生成sql语句

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
 
import java.io.*;
 
public class Main {
    public static void main(String[] args) {
        Main obj = new Main();
        File file = new File("D:\\工作表1.xls");
        obj.readExcel(file);
    }
 
    // 去读Excel的方法readExcel,该方法的入口参数为一个File对象
    public void readExcel(File file) {
        try {
            // 创建输入流,读取Excel
            InputStream is = new FileInputStream(file.getAbsolutePath());
            // jxl提供的Workbook类
            Workbook wb = Workbook.getWorkbook(is);
            // Excel的页签数量
            int sheet_size = wb.getNumberOfSheets();
            for (int index = 0; index < sheet_size; index++) {
                // 每个页签创建一个Sheet对象
                Sheet sheet = wb.getSheet(index);
                // sheet.getRows()返回该页的总行数
                StringBuilder sb = new StringBuilder();
                String str1 = "";
                String str2 = "";
                String str3 = "";
                int count = 0;
                for (int i = 0; i < sheet.getColumns(); i++) {
                    // sheet.getColumns()返回该页的总列数
                    ++count;
                    for (int j = 0; j < sheet.getRows(); j++) {
                        String cellinfo = sheet.getCell(i, j).getContents();
                        if (j == 0) {
                            str1 = cellinfo;
                        } else if (j == 1) {
                            str2 = cellinfo;
                        } else {
                            str3 = cellinfo;
                        }
                    }
                    sb.append(str1).append("  ").append(str3).append("  ").append("comment").append("'").append(str2).append("'").append(",");
                    System.out.println(sb.toString());
                    sb.setLength(0);
                    str1 = "";
                    str2 = "";
                    str3 = "";
                }
                System.out.println("总字段数:"+count);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值