Java按列讀取Excel內容

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

public class ExcelUtil {
    public static ArrayList<String> readColumn(File file, int index) throws Exception {
        ArrayList<String> res = new ArrayList<String>();
        InputStream inputStream = new FileInputStream(file.getAbsoluteFile());
        Workbook workbook = Workbook.getWorkbook(inputStream);
        Sheet sheet = workbook.getSheet(0);
        int rows = sheet.getRows();
        int columns = sheet.getColumns();
        for (int i = 1; i < rows; i++) {
            Cell cell = sheet.getCell(index, i);
            String sql="INSERT INTO `device_center`.`device` (`host_serial_id`, `type`, `classify`, `domain`, `name`, `auth_passwd`, `serial_id`, `attributes`, `status`, `host_last_heart_beat`, `manufacturer_id`, `manufacturer`, `provider`, `gmt_create`, `gmt_modify`, `online`, `online_offline_flag`, `gateway_type`, `alias_id`, `dev_addr`) VALUES ( '"+cell.getContents().trim()+"', '5006', 'device', 'smartHotel', 'moredian', NULL, '"+cell.getContents().trim()+"', NULL, NULL, NULL, '59', 'moredian', 'moredian', '2019-12-30 14:53:24', '2019-12-30 14:53:24', '1', '0', NULL, NULL, NULL)"+";";
            System.out.println(sql);
            res.add(cell.getContents());
        }
        return res;
    }

    public static void main(String[] args) {
        File file = new File("E:\\out\\6b.xls");
        try {
            readColumn(file, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章