av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Java實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件的方法(poi,jxl)

瀏覽:121日期:2022-05-26 17:59:28

目前,比較常用的實(shí)現(xiàn)Java導(dǎo)入、導(dǎo)出Excel的技術(shù)有兩種Jakarta POI和Java Excel直接上代碼:

一,POI

POI是apache的項(xiàng)目,可對(duì)微軟的Word,Excel,Ppt進(jìn)行操作,包括office2003和2007,Excl2003和2007。poi現(xiàn)在一直有更新。所以現(xiàn)在主流使用POI。

xls:

pom:

<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.9</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.2</version></dependency>

導(dǎo)出:

public class PoiCreateExcel { public static void main(String[] args) { // 創(chuàng)建表頭 String[] title = {'id','name','sex'}; //創(chuàng)建Excel工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); //創(chuàng)建一個(gè)工作表sheet HSSFSheet sheet = workbook.createSheet(); //創(chuàng)建第一行 HSSFRow row = sheet.createRow(0); HSSFCell cell = null; // 插入第一行 for (int i = 0; i < title.length; i++) { cell = row.createCell(i); cell.setCellValue(title[i]); } // 追加數(shù)據(jù) for (int i = 1; i < 10; i++) {// 這里的int 起始是1 也就是第二行開始 HSSFRow nexTrow = sheet.createRow(i); HSSFCell cell2 = nexTrow.createCell(0); cell2.setCellValue('a'+i); cell2 = nexTrow.createCell(1); cell2.setCellValue('user'); cell2 = nexTrow.createCell(2); cell2.setCellValue('男'); } // 創(chuàng)建一個(gè)文件 File file = new File('d:/poi.xls'); try { file.createNewFile(); // 將內(nèi)容存盤 FileOutputStream stream = FileUtils.openOutputStream(file); workbook.write(stream); stream.close(); } catch (Exception e) { e.printStackTrace(); } }}

導(dǎo)入:

public class PoiReadExcel { public static void main(String[] args) { // 引入需要解析的文件 File file = new File('d:/poi.xls'); try { // 創(chuàng)建Excel 讀取文件內(nèi)容 HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file)); /** * 第一種方式讀取Sheet頁(yè) */// HSSFSheet sheet = workbook.getSheet('Sheet0'); /** * 第二種方式讀取Sheet頁(yè) */ HSSFSheet sheet = workbook.getSheetAt(0); int firstRowNum = 0;// 起始行第0行 int lasrRowNum = sheet.getLastRowNum();// 一直讀到最后一行 for (int i = 0; i < lasrRowNum; i++) { HSSFRow row = sheet.getRow(i); // 獲取當(dāng)前最后單元格列號(hào) int lastCellNum = row.getLastCellNum(); for (int j = 0; j < lastCellNum; j++) { HSSFCell cell = row.getCell(j); String value = cell.getStringCellValue();// 注意! 如果Excel 里面的值是String 那么getStringCellValue 如果是其他類型 則需要修改 System.out.print(value + ' '); } System.out.println(); } } catch (Exception e) { e.printStackTrace(); } }}

xlsx:

pom:

<!-- poi高版本額外包 --><dependency><groupId>org.apache.poi</groupId><artifactId>poi-examples</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-excelant</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.9</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-scratchpad</artifactId> <version>3.9</version></dependency>

導(dǎo)出:

public class PoiCreateExcel { public static void main(String[] args) { // 創(chuàng)建表頭 String[] title = {'id','name','sex'}; //創(chuàng)建Excel工作薄 XSSFWorkbook workbook = new XSSFWorkbook(); //創(chuàng)建一個(gè)工作表shheet Sheet sheet = workbook.createSheet(); //創(chuàng)建第一行 Row row = sheet.createRow(0); Cell cell = null; // 插入第一行 for (int i = 0; i < title.length; i++) { cell = row.createCell(i); cell.setCellValue(title[i]); } // 追加數(shù)據(jù) for (int i = 1; i < 10; i++) {// 這里的int 起始是1 也就是第二行開始 Row nexTrow = sheet.createRow(i); Cell cell2 = nexTrow.createCell(0); cell2.setCellValue('a'+i); cell2 = nexTrow.createCell(1); cell2.setCellValue('user'); cell2 = nexTrow.createCell(2); cell2.setCellValue('男'); } // 創(chuàng)建一個(gè)文件 File file = new File('d:/poi.xlsx');// 這里可以修改成高版本的 try { file.createNewFile(); // 將內(nèi)容存盤 FileOutputStream stream = FileUtils.openOutputStream(file); workbook.write(stream); stream.close(); } catch (Exception e) { e.printStackTrace(); } }}

導(dǎo)入:

public class PoiReadExcel { public List<Double> readExcels(InputStream is)throws Exception{ List<Double> xlsxList = new ArrayList<Double>(); try { if(is ==null){ throw new IOException('文件不正確!'); } Workbook workbook = WorkbookFactory.create(is); FormulaEvaluator fe = workbook.getCreationHelper().createFormulaEvaluator(); //獲取第一張表 Sheet sheet = workbook.getSheetAt(0); if(sheet == null){ throw new IOException('傳入的excel的第一張表為空!'); } for(int rowNum = 0;rowNum <= sheet.getLastRowNum(); rowNum++){ Row row = sheet.getRow(rowNum); if(row != null){ //獲得當(dāng)前行的開始列 int firstCellNum = row.getFirstCellNum(); //獲得當(dāng)前行的列數(shù) int lastCellNum = row.getPhysicalNumberOfCells(); String result = ''; //循環(huán)當(dāng)前行 for(int cellNum = firstCellNum; cellNum < lastCellNum;cellNum++){ Cell cell = row.getCell(cellNum); double value = 0; String valueString = cell.getStringCellValue(); if(null!=fe.evaluate(cell)){ value = fe.evaluate(cell).getNumberValue(); } //result = result + cellNum + ':'+value + '----'; result = result + cellNum + ':'+valueString + '----'; } System.out.println(result + ' '); } } is.close(); } catch (FileNotFoundException e) { throw new Exception('文件不正確!'); } return xlsxList; } public static void main(String[] args) throws Exception { InputStream is = new FileInputStream('d:/poi.xlsx'); PoiReadExcel re = new PoiReadExcel(); re.readExcels(is); }}

二,JXL

JXL只能對(duì)Excel進(jìn)行操作,屬于比較老的框架,它只支持到Excel 95-2000的版本?,F(xiàn)在已經(jīng)停止更新和維護(hù)。

pom:

<!-- jxl --><dependency><groupId>net.sourceforge.jexcelapi</groupId><artifactId>jxl</artifactId><version>2.6.10</version></dependency>

導(dǎo)出:

public class JxlCreateExcel { public static void main(String[] args) { // 首先設(shè)置表格第一行 表格頭名稱 也就是列名 String [] title = {'id','name','sex'}; // 創(chuàng)建Excel文件 存入路徑 File file = new File('d:/jxl.xls'); try { file.createNewFile(); // 創(chuàng)建工作薄 WritableWorkbook workbook = Workbook.createWorkbook(file); // 創(chuàng)建sheet WritableSheet sheet = workbook.createSheet('sheet1',0); // 添加數(shù)據(jù) Label label = null; // 第一行設(shè)置列名 for (int i = 0; i < title.length; i++) { label = new Label(i,0,title[i]); sheet.addCell(label); } // 追加數(shù)據(jù) 從第二行開始 i從1開始 for (int i = 1; i < 9; i++) { label = new Label(0,i,'id:'+i); sheet.addCell(label); label = new Label(1,i,'user'); sheet.addCell(label); label = new Label(2,i,'男'); sheet.addCell(label); } // 寫入 并在最后關(guān)閉流 workbook.write(); workbook.close(); } catch (Exception e) { e.printStackTrace(); } }}

導(dǎo)入:

public class JxlReadExcel { public static void main(String[] args) { try { // 創(chuàng)建 Workbook Workbook workbook = Workbook.getWorkbook(new File('d:/jxl.xls')); // 獲取工作表sheet Sheet sheet = workbook.getSheet(0); // 獲取數(shù)據(jù) for (int i = 0; i < sheet.getRows(); i++) {// 獲取行 for (int j = 0; j < sheet.getColumns(); j++) {// 獲取列 Cell cell = sheet.getCell(j,i); System.out.print(cell.getContents() + ' ');// 得到單元格的內(nèi)容 } System.out.println(); } workbook.close(); } catch (Exception e) { e.printStackTrace(); } }}

到此,代碼可直接部署運(yùn)行,希望可以幫助到你~

總結(jié)

到此這篇關(guān)于Java實(shí)現(xiàn)導(dǎo)入導(dǎo)出Excel文件的方法(poi,jxl)的文章就介紹到這了,更多相關(guān)java實(shí)現(xiàn)導(dǎo)入導(dǎo)出excel文件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: excel
主站蜘蛛池模板: 中文在线播放 | 久婷婷 | 中文字幕视频在线观看免费 | 国产精品国产精品国产专区不卡 | 四虎影视免费在线 | 免费国产黄网站在线观看视频 | 久久久国产精品 | 特级特黄特色的免费大片 | 成人久久18免费网站 | 免费在线观看成人 | 亚洲精品乱码久久久久久蜜桃91 | 青青草亚洲 | 欧美日韩高清在线一区 | 国产一区二区三区在线 | 亚洲精品中文字幕av | 国产欧美在线 | 日本欧美国产 | 天天干夜夜操 | 在线国产中文字幕 | 丁香五月缴情综合网 | 欧美黑人一区二区三区 | 欧美日韩在线综合 | 精品久久久久久久久久 | 91视频一区二区三区 | 亚洲欧洲视频 | 久久国产精品精品国产色婷婷 | 欧美一区二区三区视频在线 | 中文字幕亚洲区一区二 | 日韩一区二区av | 欧美一区二区三区视频 | 中文字幕日韩av | 国产一区二区三区日韩 | 欧美日韩国产在线 | 亚洲视频三区 | 日韩欧美视频 | 日韩久久久久 | 午夜大片| a毛片 | 精品av| 中文成人在线 | 日本久久精品 |