您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“怎么把excel文件到本地數(shù)據(jù)庫”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“怎么把excel文件到本地數(shù)據(jù)庫”吧!
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的字段,對應(yīng)表頭插入
// 獲取該對象的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); // 設(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 = "測試";
// Excel文件易車sheet頁的第一行
String title[] = { "日期", "城市", "新發(fā)布車源數(shù)" };
// Excel文件易車每一列對應(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不存在,則新建一個新的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ù)庫”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。