欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么把excel文件到本地數(shù)據(jù)庫

本篇內(nèi)容主要講解“怎么把excel文件到本地數(shù)據(jù)庫”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么把excel文件到本地數(shù)據(jù)庫”吧!

成都創(chuàng)新互聯(lián)公司是工信部頒發(fā)資質(zhì)IDC服務器商,為用戶提供優(yōu)質(zhì)的服務器托管服務

package com.pingan.wiseapm.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class ExcelManage {

    private HSSFWorkbook workbook = null;

    public boolean fileExist(String filePath) {

        boolean flag = false;
        File file = new File(filePath);
        flag = file.exists();
        return flag;
    }

    public boolean sheetExist(String filePath, String sheetName) {
        boolean flag = false;
        File file = new File(filePath);
        if (file.exists()) {
            try {
                workbook = new HSSFWorkbook(new FileInputStream(file));
                // 添加Worksheet(不添加sheet時生成的xls文件打開時會報錯)
                HSSFSheet sheet = workbook.getSheet(sheetName);
                if (sheet != null)
                    flag = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else { // 文件不存在
            flag = false;
        }
        return flag;
    }

    public void createSheet(String filePath, String sheetName, String titleRow[]) throws FileNotFoundException, IOException {
        FileOutputStream out = null;
        File excel = new File(filePath); // 讀取文件
        FileInputStream in = new FileInputStream(excel); // 轉(zhuǎn)換為流
        workbook = new HSSFWorkbook(in); // 加載excel的 工作目錄

        workbook.createSheet(sheetName); // 添加一個新的sheet
        // 添加表頭
        Row row = workbook.getSheet(sheetName).createRow(0); // 創(chuàng)建第一行
        try {
            for (int i = 0; i < titleRow.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellValue(titleRow[i]);
            }
            out = new FileOutputStream(filePath);
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 創(chuàng)建新excel.
     * 
     * @param filePath
     *            excel的路徑
     * @param sheetName
     *            要創(chuàng)建的表格索引
     * @param titleRow
     *            excel的第一行即表格頭
     */
    public void createExcel(String filePath, String sheetName, String titleRow[]) {
        // 創(chuàng)建workbook
        workbook = new HSSFWorkbook();
        // 添加Worksheet(不添加sheet時生成的xls文件打開時會報錯)
        workbook.createSheet(sheetName);
        // 新建文件
        FileOutputStream out = null;
        try {
            // 添加表頭
            Row row = workbook.getSheet(sheetName).createRow(0); // 創(chuàng)建第一行
            for (int i = 0; i < titleRow.length; i++) {
                Cell cell = row.createCell(i);
                cell.setCellValue(titleRow[i]);
            }
            out = new FileOutputStream(filePath);
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 往excel中寫入.
     * 
     * @param filePath
     *            文件路徑
     * @param sheetName
     *            表格索引
     * @param object
     */
    public void writeToExcel(String filePath, String sheetName, Object object, String titleRow[]) {
        // 創(chuàng)建workbook
        File file = new File(filePath);
        try {
            workbook = new HSSFWorkbook(new FileInputStream(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileOutputStream out = null;
        HSSFSheet sheet = workbook.getSheet(sheetName);
        // 獲取表格的總行數(shù)
        int rowCount = sheet.getLastRowNum() + 1; // 需要加一
        try {
            Row row = sheet.createRow(rowCount); // 最新要添加的一行
            // 通過反射獲得object的字段,對應表頭插入
            // 獲取該對象的class對象
            Class<? extends Object> class_ = object.getClass();

            for (int i = 0; i < titleRow.length; i++) {
                String title = titleRow[i];
                String UTitle = Character.toUpperCase(title.charAt(0)) + title.substring(1, title.length()); // 使其首字母大寫;
                String methodName = "get" + UTitle;
                Method method = class_.getDeclaredMethod(methodName); // 設置要執(zhí)行的方法
                String data = method.invoke(object).toString(); // 執(zhí)行該get方法,即要插入的數(shù)據(jù)
                Cell cell = row.createCell(i);
                cell.setCellValue(data);
            }
            out = new FileOutputStream(filePath);
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        String filePath = "fileList/board1.xlsx";
        String sheetName = "測試";
        // Excel文件易車sheet頁的第一行
        String title[] = { "日期", "城市", "新發(fā)布車源數(shù)" };
        // Excel文件易車每一列對應的數(shù)據(jù)
        String titleDate[] = { "date", "city", "newPublish" };

        ExcelManage em = new ExcelManage();
        // 判斷該名稱的文件是否存在
        boolean fileFlag = em.fileExist(filePath);
        if (!fileFlag) {
            em.createExcel(filePath, sheetName, title);
        }
        // 判斷該名稱的Sheet是否存在
        boolean sheetFlag = em.sheetExist(filePath, sheetName);
        // 如果該名稱的Sheet不存在,則新建一個新的Sheet
        if (!sheetFlag) {
            try {
                em.createSheet(filePath, sheetName, title);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        YiCheData user = new YiCheData();
        user.setDate("206-12-21");
        user.setCity("北京");
        user.setNewPublish("5");
        // 寫入到excel
        em.writeToExcel(filePath, sheetName, user, titleDate);
    }

}
 

到此,相信大家對“怎么把excel文件到本地數(shù)據(jù)庫”有了更深的了解,不妨來實際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

當前名稱:怎么把excel文件到本地數(shù)據(jù)庫
網(wǎng)站網(wǎng)址:http://www.chinadenli.net/article2/gspjoc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設網(wǎng)站營銷響應式網(wǎng)站電子商務自適應網(wǎng)站營銷型網(wǎng)站建設

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設計公司