日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

java實現Excel的導入導出

瀏覽:216日期:2022-05-29 13:00:55

本文實例為大家分享了java實現Excel導入導出的具體代碼,供大家參考,具體內容如下

一.Excel讀寫技術

java實現Excel的導入導出

區別:

java實現Excel的導入導出

二.jxl讀寫基礎代碼

1.從數據庫將數據導出到excel表格

public class JxlExcel {public static void main(String[] args) { //創建Excel文件 String[] title= {'姓名','課程名','分數'}; File file=new File('f:/sheet1.xls'); try { file.createNewFile(); //創建工作簿 WritableWorkbook workbook=Workbook.createWorkbook(file); //創建Sheet WritableSheet sheet=workbook.createSheet('表格一', 20); //第一行設置列名 Label label=null; for (int i = 0; i < title.length; i++) { label=new Label(i, 0, title[i]);//第一個參數為列,第二個為行 sheet.addCell(label); } Data data=new Data(); ResultSet rs=data.getString(); while(rs.next()) { System.out.println(rs.getString(1)); label=new Label(0,rs.getRow(),rs.getString(1)); sheet.addCell(label); label=new Label(1,rs.getRow(),rs.getString(2)); sheet.addCell(label); label=new Label(2,rs.getRow(),rs.getString(3)); sheet.addCell(label); } workbook.write(); workbook.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }} }

2.從Excel表格中讀取數據

public class JxlRead {public static void main(String[] args) { //創建workbook try { Workbook workbook=Workbook.getWorkbook(new File('f:/sheet1.xls')); //獲取第一個表格 Sheet sheet=workbook.getSheet(0); //獲取數據 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()+' '); } System.out.println(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}

三.Poi讀寫基礎代碼

//所需jar包:commons-io-2.2.jar;poi-3.11-20141221.jar//通過poi進行excel導入數據public class PoiExcel {public static void main(String[] args) throws SQLException { String title[]= {'名字','課程','分數'}; //1.創建Excel工作簿 HSSFWorkbook workbook=new HSSFWorkbook(); //2.創建一個工作表 HSSFSheet sheet=workbook.createSheet('sheet2'); //3.創建第一行 HSSFRow row=sheet.createRow(0); HSSFCell cell=null; //4.插入第一行數據 for (int i = 0; i < title.length; i++) { cell=row.createCell(i); cell.setCellValue(title[i]); } //5.追加數據 Data data=new Data(); ResultSet rs=data.getString(); while(rs.next()) { HSSFRow row2=sheet.createRow(rs.getRow()); HSSFCell cell2=row2.createCell(0); cell2.setCellValue(rs.getString(1)); cell2=row2.createCell(1); cell2.setCellValue(rs.getString(2)); cell2=row2.createCell(2); cell2.setCellValue(rs.getString(3)); } //創建一個文件,將Excel內容存盤 File file=new File('e:/sheet2.xls'); try { file.createNewFile(); FileOutputStream stream=FileUtils.openOutputStream(file); workbook.write(stream); stream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}

//將Excel表中內容讀取public class PoiRead {public static void main(String[] args) { //需要解析的Excel文件 File file=new File('e:/sheet2.xls'); try { //獲取工作簿 FileInputStream fs=FileUtils.openInputStream(file); HSSFWorkbook workbook=new HSSFWorkbook(fs); //獲取第一個工作表 HSSFSheet hs=workbook.getSheetAt(0); //獲取Sheet的第一個行號和最后一個行號 int last=hs.getLastRowNum(); int first=hs.getFirstRowNum(); //遍歷獲取單元格里的信息 for (int i = first; i <last; i++) { HSSFRow row=hs.getRow(i); int firstCellNum=row.getFirstCellNum();//獲取所在行的第一個行號 int lastCellNum=row.getLastCellNum();//獲取所在行的最后一個行號 for (int j = firstCellNum; j <lastCellNum; j++) { HSSFCell cell=row.getCell(j); String value=cell.getStringCellValue(); System.out.print(value+' '); } System.out.println(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}

如果Excel版本過高則需要改寫用XSSF

public class PoiExpExcel2 { /** * POI生成Excel文件 * @author David * @param args */ public static void main(String[] args) { String[] title = {'id','name','sex'}; //創建Excel工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); //創建一個工作表sheet Sheet sheet = workbook.createSheet(); //創建第一行 Row row = sheet.createRow(0); Cell cell = null; //插入第一行數據 id,name,sex for (int i = 0; i < title.length; i++) { cell = row.createCell(i); cell.setCellValue(title[i]); } //追加數據 for (int i = 1; i <= 10; i++) { Row nextrow = sheet.createRow(i); Cell cell2 = nextrow.createCell(0); cell2.setCellValue('a' + i); cell2 = nextrow.createCell(1); cell2.setCellValue('user' + i); cell2 = nextrow.createCell(2); cell2.setCellValue('男'); } //創建一個文件 File file = new File('e:/poi_test.xlsx'); try { file.createNewFile(); //將Excel內容存盤 FileOutputStream stream = FileUtils.openOutputStream(file); workbook.write(stream); stream.close(); } catch (IOException e) { e.printStackTrace(); } } }

四.定制導入模板

1.首先準備好模板的.xml文件,然后導入所需的jar包

例子:student.xml文件

<?xml version='1.0' encoding='UTF-8'?><excel code='student' name='學生信息導入'> <colgroup> <col index='A' width=’17em’></col> <col index='B' width=’17em’></col> <col index='C' width=’17em’></col> <col index='D' width=’17em’></col> <col index='E' width=’17em’></col> <col index='F' width=’17em’></col> </colgroup> <title> <tr height='16px'> <td rowspan='1' colspan='6' value='學生信息導入' /> </tr> </title> <thead> <tr height='16px'> <th value='編號' /> <th value='姓名' /> <th value='年齡' /> <th value='性別' /> <th value='出生日期' /> <th value=' 愛好' /> </tr> </thead> <tbody> <tr firstrow='2' firstcol='0' repeat='5'> <td type='string' isnullable='false' maxlength='30' /><!--用戶編號 --> <td type='string' isnullable='false' maxlength='50' /><!--姓名 --> <td type='numeric' format='##0' isnullable='false' /><!--年齡 --> <td type='enum' format='男,女' isnullable='true' /><!--性別 --> <td type='date' isnullable='false' maxlength='30' /><!--出生日期 --> <td type='enum' format='足球,籃球,乒乓球' isnullable='true' /><!--愛好 --> </tr> </tbody></excel>

所需jar包:commons-lang3-3.1.jarjdom.jarpoi-3.11-20141221.jarcommons-io-2.2.jar

java代碼:

//準備工作:導入相關jar包commons-lang3-3.1.jar,jdom.jar,poi-3.11-20141221.jarpublic class CreateTemp {public static void main(String[] args) { //獲取解析Xml路徑 String path=System.getProperty('user.dir')+'/student.xml'; File file=new File(path); SAXBuilder builder=new SAXBuilder(); //解析xml文件 try { Document document=builder.build(file); //創建Excel HSSFWorkbook workbook=new HSSFWorkbook(); //創建表格 HSSFSheet sheet=workbook.createSheet('sheet0'); //獲取Xml文件的根節點 Element root=document.getRootElement(); //獲取模板名稱 String tempName=root.getAttributeValue('name'); //設置列寬 Element colgroup=root.getChild('colgroup'); setColumnWidth(sheet,colgroup); //設置標題 int rownum = 0; int column = 0; Element title=root.getChild('title'); List<Element> trs=title.getChildren('tr'); for (int i = 0; i <trs.size(); i++) { Element tr=trs.get(i); List<Element> tds=tr.getChildren('td'); HSSFRow row=sheet.createRow(rownum); HSSFCellStyle cellStyle=workbook.createCellStyle();//創建單元格格式 cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//標題居中 for (int j = 0; j < tds.size(); j++) { Element td=tds.get(j); HSSFCell cell=row.createCell(j); Attribute rowspan=td.getAttribute('rowspan'); Attribute colspan=td.getAttribute('colspan'); Attribute value=td.getAttribute('value'); if (value!=null) { String content=value.getValue(); cell.setCellValue(content); int rspan=rowspan.getIntValue()-1; int cspan=colspan.getIntValue()-1; //設置字體 HSSFFont font=workbook.createFont(); font.setFontName('仿宋_GB2312'); font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);//字體加粗// font.setFontHeight((short)12); font.setFontHeightInPoints((short)12); cellStyle.setFont(font); cell.setCellStyle(cellStyle); //合并單元格居中 sheet.addMergedRegion(new CellRangeAddress(rspan, rspan, 0, cspan)); } } rownum++; } //設置表頭 Element thead=root.getChild('thead'); trs=thead.getChildren('tr'); for (int i = 0; i < trs.size(); i++) { Element tr=trs.get(i); HSSFRow row=sheet.createRow(rownum); List<Element> ths=tr.getChildren('th'); for (int j = 0; j <ths.size(); j++) { Element th=ths.get(j); HSSFCell cell=row.createCell(j); Attribute value=th.getAttribute('value'); if (value!=null) { String content=value.getValue(); cell.setCellValue(content); } } rownum++; } //設置數據區域樣式 Element tbody = root.getChild('tbody'); Element tr=tbody.getChild('tr'); int repeat=tr.getAttribute('repeat').getIntValue(); List<Element> tds=tr.getChildren('td'); for (int i = 0; i < repeat; i++) { HSSFRow row=sheet.createRow(rownum); for (int j = 0; j < tds.size(); j++) { Element td=tds.get(j); HSSFCell cell=row.createCell(j); setType(workbook,cell,td); } } rownum++; //生成Excel導入模板 File tempFile=new File('e:/'+tempName+'.xls'); tempFile.delete(); tempFile.createNewFile(); FileOutputStream fos=FileUtils.openOutputStream(tempFile); workbook.write(fos); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }} private static void setType(HSSFWorkbook workbook, HSSFCell cell, Element td) { Attribute typeAttr = td.getAttribute('type'); String type = typeAttr.getValue(); HSSFDataFormat format = workbook.createDataFormat(); HSSFCellStyle cellStyle = workbook.createCellStyle(); if('NUMERIC'.equalsIgnoreCase(type)){ cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); Attribute formatAttr = td.getAttribute('format'); String formatValue = formatAttr.getValue(); formatValue = StringUtils.isNotBlank(formatValue)? formatValue : '#,##0.00'; cellStyle.setDataFormat(format.getFormat(formatValue)); }else if('STRING'.equalsIgnoreCase(type)){ cell.setCellValue(''); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cellStyle.setDataFormat(format.getFormat('@')); }else if('DATE'.equalsIgnoreCase(type)){ cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cellStyle.setDataFormat(format.getFormat('yyyy-m-d')); }else if('ENUM'.equalsIgnoreCase(type)){ CellRangeAddressList regions = new CellRangeAddressList(cell.getRowIndex(), cell.getRowIndex(), cell.getColumnIndex(), cell.getColumnIndex()); Attribute enumAttr = td.getAttribute('format'); String enumValue = enumAttr.getValue(); //加載下拉列表內容 DVConstraint constraint = DVConstraint.createExplicitListConstraint(enumValue.split(',')); //數據有效性對象 HSSFDataValidation dataValidation = new HSSFDataValidation(regions, constraint); workbook.getSheetAt(0).addValidationData(dataValidation); } cell.setCellStyle(cellStyle); } private static void setColumnWidth(HSSFSheet sheet, Element colgroup) { List<Element> cols=colgroup.getChildren('col');//獲取col的節點 for (int i = 0; i < cols.size(); i++) { Element col=cols.get(i); Attribute width=col.getAttribute('width');//獲取每列中的width屬性 String unit = width.getValue().replaceAll('[0-9,.]', '');//單位 String value = width.getValue().replaceAll(unit, '');//數值 int v=0; if(StringUtils.isBlank(unit) || 'px'.endsWith(unit)){ v = Math.round(Float.parseFloat(value) * 37F); }else if ('em'.endsWith(unit)){ v = Math.round(Float.parseFloat(value) * 267.5F); }//對單位進行判斷 sheet.setColumnWidth(i, v); } }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: excel
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲激情社区| 日本不卡视频在线| 97精品国产福利一区二区三区| 国产+成+人+亚洲欧洲在线| 亚洲一区二区小说| 九色porny丨国产首页在线| 麻豆精品蜜桃视频网站| 亚洲精品成人一区| 久久不射网站| 好吊视频一区二区三区四区| 日韩精品免费一区二区三区| 日韩av一区二区在线影视| 亚洲一区二区成人| 99xxxx成人网| 日韩中文字幕不卡| 99pao成人国产永久免费视频 | 中文字幕在线视频久| 国产精品久久久久av蜜臀| 亚洲日本国产| 日韩二区三区四区| 国产精品www994| 精品女同一区二区三区在线观看| 欧美成人aaa| 在线亚洲免费| 亚洲免费资源| 国产精品色在线网站| 日本久久综合| 欧美1区2区3区| 国产一区二区三区日韩精品| 老司机免费视频一区二区| 久久免费福利| 狠狠爱成人网| 久久精品72免费观看| 日本中文字幕不卡| 精品99久久| 9色精品在线| 国产精品免费精品自在线观看| 久久精品影视| 亚洲精品一级二级三级| 欧美精品不卡| 欧美日一区二区| 中文字幕av亚洲精品一部二部| 久久精品国产网站| 欧美成人亚洲| 久久影院资源站| 黄色亚洲免费| 三上亚洲一区二区| 欧美片网站免费| 欧美日韩水蜜桃| 精品资源在线| 亚洲一区二区三区中文字幕在线观看| 国产一区一一区高清不卡| 日韩高清不卡一区二区| 欧美二三四区| 欧美成人精品一级| 日韩精品一区二区三区中文字幕| 久久久精品午夜少妇| 不卡福利视频| 国产探花一区在线观看| 久久国产欧美| 国产精品22p| 免费人成精品欧美精品| 久久免费黄色| 超级白嫩亚洲国产第一| 亚洲精品一级| 中文字幕一区二区三区日韩精品| 免费久久久久久久久| 久久免费视频66| 日韩高清三区| 一二三区精品| 尹人成人综合网| 伊人久久婷婷| 精品欧美激情在线观看| 国产精品欧美一区二区三区不卡| 免费精品视频| 久久亚洲精品伦理| 日本v片在线高清不卡在线观看| 久久亚洲视频| 精品三级久久| 黑丝一区二区| 亚洲一区二区三区久久久| 日本a级不卡| 青青国产精品| 蜜桃视频一区二区三区| 日韩三级久久| 精品一区二区三区中文字幕视频| 麻豆成全视频免费观看在线看| 亚洲综合在线电影| 丝袜诱惑一区二区| 超碰超碰人人人人精品| 99精品综合| 亚洲精品国产精品粉嫩| 国产日韩欧美高清免费| 精品美女在线视频| 丝袜av一区| 亚洲免费观看高清完整版在线观| 欧美一级网址| 99视频精品全国免费| 在线午夜精品| 在线免费观看亚洲| 精品三级在线| 亚洲免费成人| 欧美黑人巨大videos精品| 国产一区清纯| 日韩一区二区久久| 久久精品国产999大香线蕉| 日产精品一区| 国产亚洲人成a在线v网站| 日本欧美不卡| 久久99久久人婷婷精品综合| 激情五月综合网| 久久精品国产一区二区| 国产视频一区免费看| 高清日韩欧美| 国产伦理久久久久久妇女| 国产成人久久精品一区二区三区| 亚洲欧美网站在线观看| 日韩精品欧美激情一区二区| 国产精品视频3p| 国产一区亚洲| 国产一区二区三区四区五区| 亚洲精品观看| 亚洲高清激情| 天堂а√在线最新版中文在线| 久久国产精品色av免费看| 午夜精品婷婷| 亚洲手机视频| 亚洲夜间福利| 日韩在线二区| 国产成人免费精品| 精品视频自拍| 国产精品亚洲人成在99www| 国产麻豆久久| av高清不卡| 91精品国产自产观看在线| 日本欧美一区二区在线观看| 日韩在线a电影| 日韩va亚洲va欧美va久久| 亚洲一区二区三区四区电影 | 国产在线观看www| 国产劲爆久久| 国产欧美视频在线| 免费在线亚洲欧美| 日韩不卡手机在线v区| 国产免费av国片精品草莓男男| 国产精品s色| 久久一区视频| 国产欧洲在线| 国产精品毛片| 日韩不卡一区二区| 久久免费福利| 一区三区视频| 涩涩涩久久久成人精品| 国产aⅴ精品一区二区三区久久| 精品精品99| 免费国产自久久久久三四区久久| 久久国产成人| 久久成人高清| 在线成人直播| 亚洲一区二区三区久久久| 国产精品jk白丝蜜臀av小说| 日韩大片在线观看| 伊人久久亚洲美女图片| 国产精品极品在线观看| 国产999精品在线观看| 国产一区二区三区四区大秀| 天堂成人免费av电影一区| 国产精品视频一区二区三区四蜜臂| 久久精品免费一区二区三区| 日韩精品免费视频一区二区三区| 国产精品成久久久久| 婷婷亚洲五月| 麻豆精品在线| 亚洲狼人精品一区二区三区| 国产黄大片在线观看| 亚洲日本国产| 中文另类视频| 日本精品另类| 欧美日韩国产高清| 亚洲综合电影| 久久免费精品| 国产福利亚洲| 欧美精品福利| 蜜桃久久av一区| 成人小电影网站| 麻豆一区在线| 国产欧美综合一区二区三区| 丝袜美腿成人在线| 亚洲精品成人| 日韩国产激情| 亚洲毛片在线| 免费日韩一区二区| 成人小电影网站| 日韩成人免费| 精精国产xxxx视频在线野外| 福利一区二区免费视频| 久久国产精品久久久久久电车| 欧美日韩激情| 红桃视频欧美| 蜜桃av一区二区在线观看|