透過Java解析Excel檔案
也是一個工作中遇到的情境,很多單位想讓內部人員利用Excel維護資料(因為維護上比較簡單方便),且想讓這份資料透過網頁呈現給一般網友閱讀,所以整體後端的流程應該是:
- 將維護的Excel透過網頁介面上傳
- 將上傳的Excel轉換成XML
- 網頁去讀取這份XML來呈現
透過以上的三個步驟,就可以完成客戶想要效果。
本範例展示的是讀取Excel的XLS格式(新版Excel為XLSX格式),並轉換成陣列的方法(寫入XML在前面的範例就有了),我們選用讀取Excel的Library是Apache POI,而檔內已經有以下的資料,其中Total是公式欄位,計算Price x Quantity:
ID | Name | Price | Quantity | Total |
100001 | 產品名稱1 | 10 | 5 | 50 |
100002 | 產品名稱2 | 20 | 10 | 200 |
100003 | 產品名稱3 | 30 | 15 | 450 |
以下是讀取的程式範例:
package CDIT.stanley; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.POIFSFileSystem; public class excelToXMLFullSample { static ArrayList<ArrayList<String>> excelData = new ArrayList<ArrayList<String>>(); public static void main (String[] args) throws FileNotFoundException{ excelData.clear(); String xlsPath = "C:\\Projects\\Javas\\sample.xls"; InputStream inputStream = null; inputStream = new FileInputStream (xlsPath); POIFSFileSystem fileSystem = null; try { fileSystem = new POIFSFileSystem (inputStream); @SuppressWarnings("resource") HSSFWorkbook workBook = new HSSFWorkbook (fileSystem); HSSFSheet sheet = workBook.getSheetAt (0); Iterator<?> rows = sheet.rowIterator (); while (rows.hasNext ()){ HSSFRow row = (HSSFRow) rows.next(); Iterator<?> cells = row.cellIterator (); ArrayList<String> rowData = new ArrayList<String>(); while (cells.hasNext ()){ HSSFCell cell = (HSSFCell) cells.next(); switch (cell.getCellTypeEnum()){ case STRING :{ rowData.add(cell.getStringCellValue()); break; } case NUMERIC : { rowData.add((int)cell.getNumericCellValue() + ""); break; } case FORMULA :{ switch(cell.getCachedFormulaResultTypeEnum()) { case STRING: rowData.add(cell.getStringCellValue ()); break; case NUMERIC: rowData.add((int)cell.getNumericCellValue() + ""); break; default: rowData.add(""); break; } } default: rowData.add(""); break; } } excelData.add(rowData); } } catch(IOException e){ System.out.println("IOException " + e.getMessage()); System.out.println("轉換失敗,請檢查Excel檔案與格式是否正確"); } for(int i=0 ; i<excelData.size(); i++){ System.out.println("Excel Row "+ i +" Data : " + excelData.get(i)); } } }
程式會把Excel內的資料轉換為ArrayList,輸出內容如下:
Excel Row 0 Data : [Product ID, Product Name, Product Price, Quantity, Total] Excel Row 1 Data : [100001, 產品名稱1, 10, 5, 50, ] Excel Row 2 Data : [100002, 產品名稱2, 20, 10, 200, ] Excel Row 3 Data : [100003, 產品名稱3, 30, 15, 450, ]
открыть замок услуга http://vskrytie-zamkov-moskva113.ru/ .
What’s up to all, how is everything, I think every one is getting more from this web page, and your views are good in favor of new visitors.
forexallnews.com/broker-profile/ace-investing
wine.historic.ru/books/c0007_1.shtml
http://www.tenku.eu/pl/wpisy/nowa-porcja-onigiri?page=59
russiafreedom.ru/question/titan-s-teplakom/
ilonka.ru/user/robertgneida/
вскрыть замок цена https://vskrytie-zamkov-moskva113.ru/ .
вскрыть дверь фирмы http://vskrytie-zamkov-moskva113.ru .
Kylian Mbappe https://kylianmbappe.prostoprosport-ar.com is a French footballer, striker for Paris Saint-Germain and captain of the French national team. He began playing football in the semi-professional club Bondi, which plays in the lower leagues of France. He was noticed by Monaco scouts, which he joined in 2015 and that same year, at the age of 16, he made his debut for the Monegasques. The youngest debutant and goal scorer in the club’s history.
Pin Up Azerbaycan ?Onlayn Kazino: Pin Up Kazino ?Onlayn – Pin-up Giris
Vinicius Junior https://viniciusjunior.prostoprosport-ar.com is a Brazilian and Spanish footballer who plays as a striker for Real Madrid and the Brazilian national team. Junior became the first player in the history of Los Blancos, born in 2000, to play an official match and score a goal.
http://gkjwonosobo.or.id/2017/07/10/kunjungan-dan-wisata-rohani-kompa-gkj-wonosobo-ke-gkj-kutoarjo/?replytocom=89335
https://autolux-azerbaijan.com/# pin-up360
мониторинг серверов л2
Сервера л2
мониторинг серверов ла2
Сервера ла2
сервера ла2
Сервера л2
Pin Up Azerbaycan ?Onlayn Kazino: pin-up360 – Pin-Up Casino
https://autolux-azerbaijan.com/# Pin-up Giris
zithromax 25mg
Hello, every time i used to check blog posts here in the early hours in the daylight, for the reason that i like to gain knowledge of more and more.
1-carat.ru/
petkit.com.cn/member/index.php?uid=ehalipa&action=viewarchives&aid=6644
startspresto.ru/dannye/pluginclass/cbblogs?action=blogs&func=show&id=22
scientistsufo.ru/page/14
fastpas.info/
https://autolux-azerbaijan.com/# pin-up kazino
Pin Up Kazino ?Onlayn: Pin Up Azerbaycan – pin-up360
https://autolux-azerbaijan.com/# pin-up 141 casino
The unique viewpoints you bring to The writing are as captivating as The online presence. Always a pleasure.
?Onlayn Kazino: Pin Up Azerbaycan ?Onlayn Kazino – Pin Up
https://autolux-azerbaijan.com/# Pin Up Azerbaycan ?Onlayn Kazino
The work is truly inspirational. It’s as if you’ve found a way to whisper sweet nothings to my intellect.
https://autolux-azerbaijan.com/# pin-up360
Provoked thought and taught me something new, as if my brain needed more exercise.
pin-up360: Pin up 306 casino – Pin Up Azerbaycan
Hi! This is kind of off topic but I need some help from an established blog. Is it very hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about making my own but I’m not sure where to begin. Do you have any tips or suggestions? Appreciate it
hrcspb.ru/page/page60.html
zakon122.ru/promotion/
dbecosmeticos.com.br/blog/suplemento-alimentar-hair-supply/montazh-kondicionera-moskva.ru
http://www.sendungsverfolgung24.org/forum/topic/dfdg234dsfsd/?part=9324
travel.bizph.com/viewtopic.php?f=127&p=1031687
zyprexa 2,5mg united states
образование купить диплом какие образование купить диплом какие .
купить аттестат с реестром купить аттестат с реестром .