java實現Excel檔案的匯入匯出
java實現對Excel檔案的操作主要是通過POI來實現的
1. POI簡介
Apache POI是Apache軟體基金會的開放原始碼函式庫,POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。更多詳情可以[百度](http://www.baidu.com) 基本功能: HSSF - 提供讀寫Microsoft Excel格式檔案(.xls)的功能。 XSSF - 提供讀寫Microsoft Excel OOXML格式檔案(.xlsx)的功能。 HWPF - 提供讀寫Microsoft Word格式檔案的功能。 HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能。 HDGF - 提供讀寫Microsoft Visio格式檔案的功能。
2. 使用poi建立excel檔案
2.1 建立Excel文件演示如何利用Jakarta POI API 建立Excel 文件。(百度百科示例的修改版)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateXL {
/** Excel 檔案要存放的位置,假定在D盤下*/
public static String outputFile = "D:/test.xls";
public static void main(String argv[]) {
new CreateXL().createExcelFile("test1","xls");
new CreateXL().createExcelFile("test1","xlsx");
}
public void createExcelFile(String proFileName,String suffix){
String fileName = proFileName+"."+suffix;
String path = "D:/"+fileName;
Workbook workbook = null;
if("xls".equals(suffix)){
// 建立新的Excel97-2003 工作簿
workbook = new HSSFWorkbook();
}else if("xlsx".equals(suffix)){
// 建立新的ExcelOOXML格式工作簿
workbook = new XSSFWorkbook();
}else{
System.out.println("無後綴名");
return ;
}
//建立工作表,xls字尾為HSSFSheet,xlsx為XSSFSheet
Sheet sheet = workbook.createSheet();
// 在索引0的位置建立行(最頂端的行),xls字尾為HSSFRow,xlsx為XSSFRow
Row row = sheet.createRow(0);
//在索引0的位置建立單元格(左上端),xls字尾為HSSFCell,xlsx為XSSFCell
Cell cell = row.createCell(0);
// 定義單元格為字串型別,xls字尾為HSSFCell.CELL_TYPE_STRING,xlsx為XSSFCell.CELL_TYPE_STRING
cell.setCellType(Cell.CELL_TYPE_STRING);
// 在單元格中輸入一些內容
cell.setCellValue("增加值");
//在單元格中顯示不同的字型
// String[] subStr = {
// "first", "second"
// };
// Font ftRed = workbook.createFont();
// ftRed.setColor(Font.COLOR_RED);
// Font ftBlue = workbook.createFont();
// ftBlue.setColor(HSSFColor.BLUE.index);
// String [] subStr = {"first","second"};
// String sText = subStr[0] + "," + subStr[1];
// HSSFRichTextString textString = new HSSFRichTextString(sText);
// textString.applyFont(
// sText.indexOf(subStr[0]),
// sText.indexOf(subStr[0]) + subStr[0].length(),
// ftRed
// );
// textString.applyFont(
// sText.indexOf(subStr[1]),
// sText.indexOf(subStr[1]) + subStr[1].length(),
// ftBlue
// );
// cell.setCellValue(textString);
//建立文字樣式
CellStyle style = workbook.createCellStyle();
//設定邊框
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setBorderBottom(CellStyle.BORDER_THIN);
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//文字換行
style.setWrapText(true);
//字型
Font font = workbook.createFont();
//文字大小
font.setFontHeightInPoints((short) 12);
font.setFontName("黑體");
font.setColor(Font.COLOR_RED);
style.setFont(font);
cell.setCellStyle(style);
// 新建一輸出檔案流
FileOutputStream fOut;
try {
fOut = new FileOutputStream(path);
workbook.write(fOut);
fOut.flush();
// 操作結束,關閉檔案
fOut.close();
System.out.println("檔案生成...");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 把相應的Excel 工作簿存檔
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.2 POI操作現有本地Excel
public void changeLocalExcel(){
//找到該類同資料夾下的檔案,getResource("")是得到該類.class檔案的URL
//getResource("/")是得到該類.class檔案根目錄的URL,web專案一般為.../WEB_INF/classes
//得到檔案的輸入流
InputStream fis = CreateXL.class.getResourceAsStream("local1.xlsx");
//或者
// try {
// InputStream fs = new FileInputStream("local1.xlsx");
// POIFSFileSystem ps =new POIFSFileSystem(fs);
// Workbook workbook = new HSSFWorkbook(ps);
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
XSSFWorkbook workbook = null;
try {
//將輸入流作為引數傳入Workbook建構函式
workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
//在excel檔案最後一行後建立行
XSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+1);
XSSFCell idCell = dataRow.createCell(0);
//............各種操作
if(workbook!=null){
//輸出到檔案
FileOutputStream fos =new FileOutputStream("local1.xlsx");
workbook.write(fos);
fos.flush();
fos.close();
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
3. 瀏覽器檔案下載功能實現:
//下載檔案功能
public String downHistoryExport(String fileName){
//
try {
String excelFile = fileName;
String localFilePath = "/ExcelExport/"+excelFile;
//如果檔名引數是中文,且通過url傳參,需要轉換編碼方式,gb2312也可換成GBK
String iso_excelFile = new String( excelFile.getBytes("gb2312"), "ISO8859-1" );
HttpServletResponse response = getResponse();
response.reset();
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment;fileName=\"" + iso_excelFile+"\"");
ServletOutputStream out = null;
InputStream inputStream = ServletActionContext.getServletContext().getResourceAsStream(localFilePath);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[100];
int count = -1;
while ((count = inputStream.read(data, 0, 100)) != -1) {
outStream.write(data, 0, count);
}
out = response.getOutputStream();
outStream.writeTo(out);
out.flush();
outStream.flush();
return null;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
3.1 注意事項
(1) 在Struts2框架下的getRequest()和getReaponse()方法,需要類中繼承ActionSupport(com.opensymphony.xwork2.ActionSupport)
protected HttpServletRequest getRequest() {
return Struts2Utils.getRequest();
}
protected HttpServletResponse getResponse() {
return Struts2Utils.getResponse();
}
protected HttpSession getHttpSession() {
return Struts2Utils.getSession();
}
(2) 不能通過AJAX直接連結下載的URL,因為AJAX不會處理返回的下載內容,需要用瀏覽器連結下載的URL,在js檔案中用window.open(url)
,如果用url傳中文引數,需要轉碼var url = encodeURI("....含中文的url....")
相關推薦
js實現Excel檔案匯入匯出、利用 js-xlsx 實現 Excel 檔案匯入匯出-功能示例
1.匯入功能實現 下載js-xlsx到dist複製出xlsx.full.min.js引入到頁面中 然後通過FileReader物件讀取檔案利用js-xlsx轉成json資料 <!DOCTYPE html> <html> <head>
純前端利用 js-xlsx 實現 Excel 檔案匯入匯出功能示例
1.匯入功能實現 下載js-xlsx到dist複製出xlsx.full.min.js引入到頁面中 然後通過FileReader物件讀取檔案利用js-xlsx轉成json資料 程式碼實現(==>示例<==) <!DOCTYPE html> <html> <he
java實現Excel檔案的匯入匯出
java實現對Excel檔案的操作主要是通過POI來實現的 1. POI簡介 Apache POI是Apache軟體基金會的開放原始碼函式庫,POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。更多詳情可以[百度
Java實現對Excel檔案匯入匯出
1.匯入jar包 2.建立entity類 public class Book { private String name; private double price; private String author; public String getNa
【Java】SpringMVC整合poi實現excel的匯入匯出
2.特點:結構: HSSF - 提供讀寫Microsoft Excel格式檔案的功能。 XSSF - 提供讀寫Microsoft Excel OOXML格式檔案的功能。 HWPF - 提供讀寫Microsoft Word格式檔案的功能。 HSLF - 提供讀寫Microsof
JAVA實現資料庫資料匯入/匯出到Excel(POI技術)
準備工作: 1.匯入POI包:POI下載地址:http://download.csdn.net/detail/zxm1306192988/9522142(重要) 如下 2.匯入匯出到Excel工具類ExcelUtil.java,封裝了POI對Excel的操作 pa
Java實現Excel的匯入和匯出
一、下載jxl.jar包匯入到工程中 二、新建Book.java package com.cc.reflection; public class Book { private int id; private String name; private Strin
利用js-xlsx.js外掛實現Excel檔案匯入並解析Excel資料成json資料格式
<!--本文轉載於網路,有太多一樣的文章,不知道原作者是哪位了,就不註明出處了。這裡記載下來,用於自己的學習借鑑--><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8">
java實現excel的匯入
package com.nchu.wechatOrder.controller; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException
java 中Excel的匯入匯出
部分轉發原作者https://www.cnblogs.com/qdhxhz/p/8137282.html雨點的名字 的內容 java程式碼中的匯入匯出 首先在d盤建立一個xlsx檔案,然後再進行一系列操作 package com.aynu.excel; import j
SpringBoot整合poi實現Excel的 匯入/匯出
新增依賴 <!-- excel匯出工具 --> <dependency> <groupId>org.apache.poi</groupId>
Java實現Excel批量匯入資料
Excel的批量匯入是很常見的功能,這裡採用 Jxl實現,資料量或樣式要求較高可以採用 poi 框架環境:Spring + SpringMvc(註解實現) 首先匯入依賴jar包 <dependency> <groupId>ne
js-xlsx實現Excel的匯入匯出功能
一:匯入功能 讀取excel的多個sheet資料 <script type="text/javascript" src="js/xlsx.core.min.js"></script> <table style="width
web專案實現Excel資料匯入匯出
由於專案要求,需要實現一個數據庫資訊匯出為Excel檔案,並能將Excel檔案中的資訊匯入資料庫的功能,網上找了一下資料,發現大都只涉及到Excel檔案的簡單操作,所以特地在此分享了自己寫的兩個簡單的Web端Excel檔案匯入匯出的例子。 涉
springboot+easypoi實現Excel的匯入匯出
1.pom引入 <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base<
利用POI實現Excel的匯入匯出
在專案開中,我們經常需要用excel來匯入匯出資料,而POI技術是實現我們需求的一個選擇。 POI支援office的所有版本 POI全稱 PoorObfuscation Implementation,直譯為“可憐的模糊實現”,利用POI介面可以通過JAVA操作Micro
利用 js-xlsx 實現 Excel 檔案匯入並解析Excel資料成json格式的資料
原文出自http://www.jianshu.com/p/74d405940305,摘取了其中的一段,並做了相應的修改 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"
Android中如何實現excel的匯入/匯出
在Android中使用第三方庫來實現對excel的檔案的匯入匯出 準備操作:從網路上下載第三方庫jxl.jar並且匯入到Android studio中 一、將記憶體中的資料匯出到Excel檔案中。
phpExcel和jquery實現excel檔案匯入
前端匯入外掛:webuploader webuploader使用:使用Web Uploader檔案上傳需要引入三種資源:JS, CSS, SWF。 其中css檔案、js檔案均可以根據需求修改 前端js: function importExcel() {
JavaWeb 實現Excel 的匯入匯出
開發環境: myeclipse10.4 /** 匯出Excel 檔案 */ public String exportDictEntryData(DictionaryManagedForm fm,