1.Echarts:javascript
demo.js: html
function demo(selector){ var myEcharts=echarts.init(selector); var option = { title : { text: '某站點用戶訪問來源', subtext: '純屬虛構', x:'center' }, tooltip : { trigger: 'item', formatter: "{a} <br/>{b} : {c} ({d}%)" }, legend: { orient: 'vertical', left: 'left', data: ['直接訪問','郵件營銷','聯盟廣告','視頻廣告','搜索引擎'] }, series : [ { name: '訪問來源', type: 'pie', radius : '55%', center: ['50%', '60%'], data:[ {value:335, name:'直接訪問'}, {value:310, name:'郵件營銷'}, {value:234, name:'聯盟廣告'}, {value:135, name:'視頻廣告'}, {value:1548, name:'搜索引擎'} ], itemStyle: { emphasis: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } } } ] }; myEcharts.setOption(option); }
echarts.html:java
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"></meta> <title></title> <script type="text/javascript" src="echarts.common.min.js"></script> <script type="text/javascript" src="demo.js"></script> <script type="text/javascript"> function test(){ demo(document.getElementById("d")); } </script> </head> <body> <input type="button" id="test" value="測試" onclick="test()"/> <div id="d" style="border:1px; height:500px; width:500px"></div> </body> </html>
2.POI:linux
POIDemo1.java:數據庫
package com.zzsxt.lee.poi.demo; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * @description 寫入並建立xls文檔 * @author Seven Lee * @date 2017年11月7日 下午1:41:25 * */ public class POIDemo1 { public static void main(String[] args) { // 指定目標路徑和文件名 // windows的路徑和linux以及mac的路徑是不同 // C:\\test\test.xls // 通常狀況下能夠使用File.separator-->\,/ File file = new File("D:/123/kuang.xls"); try { // 建立輸出流(寫入本地磁盤) OutputStream outputStream = new FileOutputStream(file); /** * poi提供 */ // 在目標路徑建立目標名稱的excel文件 // HSSFWorkbook:操做excel表格的 // 經過file的路徑信息和文件名稱,在目標路徑建立出目標名稱的excel文件 HSSFWorkbook workBook = new HSSFWorkbook(); // 建立excel工做表,並建立該工做頁的主題名 // 建立表格的一個工做薄,並起名字爲user HSSFSheet sheet = workBook.createSheet("user"); // 從數據庫中查詢出了數據List<> List<User> userList = new ArrayList<User>(); for (int j = 0; j < 10; j++) { User user = new User("zhangsan" + j, "123456" + j); userList.add(user); } User user = new User(); for (int i = -1; i < userList.size(); i++) { /** * 第一次for循環: * i--> -1 * 第二次循環: * i--> 0 */ // sheet.createRow(i+1):建立第一行 // sheet.createRow(i+1):建立第二行 HSSFRow row = sheet.createRow(i+1); for(int k = 0; k < user.getClass().getDeclaredFields().length; k++) { if(i == -1) { if(k == 0) { continue; } row.createCell(k-1).setCellValue((user.getClass().getDeclaredFields()[k]).getName()); continue; } if(((user.getClass().getDeclaredFields()[k]).getName()).equals("serialVersionUID")) { continue; } Field f = userList.get(i).getClass().getDeclaredFields()[k]; f.setAccessible(true); row.createCell(k-1).setCellValue((String)f.get(userList.get(i))); } } // // 爲工做表新增長一行(第一行) // HSSFRow row1 = sheet.createRow(0); // // 在指定的行上增長兩個單元格 // row1.createCell(0).setCellValue("name"); // row1.createCell(1).setCellValue("password"); // // // 爲工做表新增長一行(第二行) // HSSFRow row2 = sheet.createRow(1); // row2.createCell(0).setCellValue("zhangsan"); // row2.createCell(1).setCellValue("123456"); // 調用輸出流把excel表格寫入硬盤中 workBook.write(outputStream); // 關閉輸出流 outputStream.close(); System.out.println("生成excel成功"); } catch (Exception e) { e.printStackTrace(); } } }
POIDemo2.java:apache
package com.zzsxt.lee.poi.demo; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import java.lang.reflect.Field; 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; /** * @description 經過java程序來從xls文檔中讀取出數據 * @author Seven Lee * @date 2017年11月7日 下午3:50:18 * */ public class POIDemo2 { public static void main(String[] args) { try { // 建立輸入流,從磁盤中獲取該文件的流信息 InputStream inputStream = new FileInputStream("D:/123/kuang.xls"); /** * poi提供 */ // 把從磁盤中讀取出的流信息保存進緩衝區 BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream); // 建立POIFSFileSystem,對該文件進行解析 // 對該文件的流進行解析 POIFSFileSystem fileSystem = new POIFSFileSystem(bufferedInputStream); // 從目標路徑的目標名稱獲取excel的文件信息-->經過fileSystem進行獲取 HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileSystem); // 根據工做薄的name獲取到目標的excel信息 HSSFSheet sheet = hssfWorkbook.getSheet("user"); User user = new User(); for (int i = 0;; i++) { if (i == 0) { continue; } HSSFRow row = sheet.getRow(i);// 獲取行信息 if (row != null) { // 須要把全部的數據從excel中取出來 // getLastCellNum():excel表中有效單元格的列數 for (int j = 0; j < row.getLastCellNum(); j++) { String excelValue = row.getCell(j).getStringCellValue(); Field[] fields = user.getClass().getDeclaredFields(); fields[j].setAccessible(true); fields[j].set(user, excelValue); } bufferedInputStream.close(); } else { bufferedInputStream.close(); return; } System.out.println(user); } } catch (Exception e) { e.printStackTrace(); } } }
ReadDocFile.java:windows
package com.zzsxt.lee.poi.demo; import java.io.FileInputStream; import java.io.FileNotFoundException; import org.apache.poi.hwpf.extractor.WordExtractor; public class ReadDocFile { public static String readWord(String pathAndName) { FileInputStream in; String text = null; try { in = new FileInputStream(pathAndName); WordExtractor extractor = new WordExtractor(in); text = extractor.getText(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return text; } }
User.java:app
package com.zzsxt.lee.poi.demo; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 5157419433803033003L; private String name; private String password; public User() { } public User(String name, String password) { this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "User [name=" + name + ", password=" + password + "]"; } }
pom.xml:echarts
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zzsxt.lee.poi.demo</groupId> <artifactId>excel_demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>excel_demo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 操做excel --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <!-- 操做work和ppt --> <dependency> <groupId>poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.1-FINAL</version> </dependency> </dependencies> <build> <finalName>excel_demo</finalName> </build> </project>
POIDemo3:建立excel 2007 xls格式xss
package com.zskj; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; 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.ss.usermodel.Font; public class Test { public static void main(String[] args) { Set <String> set = new HashSet<String>(); set.add("123"); set.add("456"); set.add("789"); set.add("666"); set.add("777"); createExcel("D:\\test\\", "aa.xls","測試",set); } /** * * @param path 文件存放路徑 * @param fileName 文件名稱 * @param sheetName 表名稱 * @param set 數據 */ public static void createExcel(String path, String fileName,String sheetName,Set<String> set) { // 第一步建立workbook HSSFWorkbook wb = new HSSFWorkbook(); // 第二步建立sheet HSSFSheet sheet = wb.createSheet(sheetName); // 第三步建立行row:添加表頭0行 HSSFRow row = sheet.createRow(0); HSSFCellStyle style = wb.createCellStyle(); HSSFFont hssfFont = wb.createFont(); // 設置字體顏色 //hssfFont.setColor(HSSFColor.BLUE.index); //字體大小 hssfFont.setFontHeightInPoints((short)11); //字體 hssfFont.setFontName("宋體"); //粗體 hssfFont.setBoldweight(Font.BOLDWEIGHT_BOLD); style.setFont(hssfFont); //居中 // style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 第四步建立單元格 HSSFCell cell = row.createCell(0); // 第一個單元格 cell.setCellValue("姓名"); cell.setCellStyle(style); cell = row.createCell(1); // 第二個單元格 cell.setCellValue("年齡"); cell.setCellStyle(style); // 第五步插入數據 Iterator<String> is = set.iterator(); int count = 1; while(is.hasNext()){
//此處注意分開寫 row = sheet.createRow(count); HSSFCell hc = orow.createCell(0);
hc.setCellValue(is.next()); count++; } // 第六步將生成excel文件保存到指定路徑下 try { FileOutputStream fout = new FileOutputStream(path + fileName); wb.write(fout); fout.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Excel文件生成成功..."); } }
POIDemo4:建立excel xlsx格式:
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * * @param path 文件存放路徑 * @param fileName 文件名稱 * @param sheetName 表名稱 * @param set 數據 */ public static void createExcel(String fileName,String path,Set<String>set,String sheetName){ //建立工做簿 XSSFWorkbook xw = new XSSFWorkbook(); //建立表 XSSFSheet xf = xw.createSheet(sheetName); //建立字體 XSSFFont xft = xw.createFont(); //設置字體 xft.setFontName("微軟雅黑"); //建立樣式 XSSFCellStyle xcs = xw.createCellStyle(); //第一行設置爲列名 XSSFRow xr = xf.createRow(0); XSSFCell xc = xr.createCell(0); xc.setCellValue("姓名"); xcs.setFont(xft); xc.setCellStyle(xcs); Iterator<String> is = set.iterator(); int count = 1; while(is.hasNext()){ xr = xf.createRow(count); xc = xr.createCell(0); xc.setCellValue(is.next()); xc.setCellStyle(xcs); count++; } //合併單元格 參數說明:1:開始行 2:結束行 3:開始列 4:結束列 xf.addMergedRegion(new CellRangeAddress(count+2,count+2,0,100)); XSSFRow count2 = xf.createRow(count+2); count2.createCell(0).setCellValue("總計"+(count-1)); File f = new File("D:\\test\\fontstyle.xlsx"); try { FileOutputStream fos = new FileOutputStream(f); xw.write(fos); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Excel建立完成......"); }
POIDemo4:讀取excel xls格式:
import java.io.File; import java.io.FileInputStream; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; // 讀取,所有sheet表及數據 public static void showExcel() throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File("D:/test/t1.xls"))); HSSFSheet sheet = null; for (int i = 0; i < workbook.getNumberOfSheets(); i++) {// 獲取每一個Sheet表 sheet = workbook.getSheetAt(i); for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {// getLastRowNum,獲取最後一行的行標 HSSFRow row = sheet.getRow(j); if (row != null) { for (int k = 0; k < row.getLastCellNum(); k++) {// getLastCellNum,是獲取最後一個不爲空的列是第幾個 if (row.getCell(k) != null) { // getCell 獲取單元格數據 System.out.print(row.getCell(k) + "\t"); } else { System.out.print("\t"); } } } System.out.println(""); // 讀完一行後換行 } System.out.println("讀取sheet表:" + workbook.getSheetName(i) + " 完成"); } } // 讀取,指定sheet表及數據 public static void showExcel2() throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(new File("D:/test/t1.xls"))); HSSFSheet sheet = null; int i = workbook.getSheetIndex("xt"); // sheet表名 sheet = workbook.getSheetAt(i); for (int j = 0; j < sheet.getLastRowNum() + 1; j++) {// getLastRowNum // 獲取最後一行的行標 HSSFRow row = sheet.getRow(j); if (row != null) { for (int k = 0; k < row.getLastCellNum(); k++) {// getLastCellNum // 是獲取最後一個不爲空的列是第幾個 if (row.getCell(k) != null) { // getCell 獲取單元格數據 System.out.print(row.getCell(k) + "\t"); } else { System.out.print("\t"); } } } System.out.println(""); } }