Excel快速生成sql语句

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weienjun/article/details/82556929
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();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weienjun/article/details/82556929