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

怎么把excel文件到本地?cái)?shù)據(jù)庫(kù)

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

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

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時(shí)生成的xls文件打開(kāi)時(shí)會(huì)報(bào)錯(cuò))
                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); // 添加一個(gè)新的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
     *            要?jiǎng)?chuàng)建的表格索引
     * @param titleRow
     *            excel的第一行即表格頭
     */
    public void createExcel(String filePath, String sheetName, String titleRow[]) {
        // 創(chuàng)建workbook
        workbook = new HSSFWorkbook();
        // 添加Worksheet(不添加sheet時(shí)生成的xls文件打開(kāi)時(shí)會(huì)報(bào)錯(cuò))
        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中寫(xiě)入.
     * 
     * @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); // 最新要添加的一行
            // 通過(guò)反射獲得object的字段,對(duì)應(yīng)表頭插入
            // 獲取該對(duì)象的class對(duì)象
            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()); // 使其首字母大寫(xiě);
                String methodName = "get" + UTitle;
                Method method = class_.getDeclaredMethod(methodName); // 設(shè)置要執(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 = "測(cè)試";
        // Excel文件易車(chē)sheet頁(yè)的第一行
        String title[] = { "日期", "城市", "新發(fā)布車(chē)源數(shù)" };
        // Excel文件易車(chē)每一列對(duì)應(yīng)的數(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不存在,則新建一個(gè)新的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");
        // 寫(xiě)入到excel
        em.writeToExcel(filePath, sheetName, user, titleDate);
    }

}
 

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

當(dāng)前名稱:怎么把excel文件到本地?cái)?shù)據(jù)庫(kù)
網(wǎng)站網(wǎng)址:http://chinadenli.net/article2/gspjoc.html

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

廣告

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

成都網(wǎng)頁(yè)設(shè)計(jì)公司