須要導入的jar包:java
package com.huawei.excel;apache
import java.io.FileOutputStream;xss
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;測試
public class TestExcel {this
/**
* 一個excel
*
* excel版本
* 97版本:指的是 能夠用office 2007之前的版本打開
* 07版本:指的是 只能由 office 2007以及之後的版本能打開
*
* 一個excel包括 一個工做簿 ,工做簿裏面有 工做表 , 而工做簿裏面有表格數據
*
*/
public static void main(String[] args) throws Exception {
// Workbook wb = new HSSFWorkbook();
Workbook wb = new XSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("F:/workbook.xlsx");
wb.write(fileOut);
fileOut.close();
}
}excel
package com.huawei.excel;orm
import java.io.File;
import java.io.FileOutputStream;blog
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;get
public class TestExcel01 {it
public static void main(String[] args) throws Exception {
/**
* 首先 建立一個工做簿
*
* 再在工做簿裏面去建立工做表
*
* 在工做表裏面建立一行
*
* 在一行中建立 單元格
*/
//建立97版本的 excel
Workbook wb = new HSSFWorkbook();
//建立一個Sheet 工做表
Sheet sheet = wb.createSheet("Hello Sheet");
//建立一行
Row row = sheet.createRow(0);
//建立一個單元格
Cell cell = row.createCell(0);
//設置單元格的內容
cell.setCellValue("this is a first excel");
FileOutputStream out = new FileOutputStream(new File("F:/first.xls"));
wb.write(out);
out.flush();
out.close();
wb.close();
}
}
package com.huawei.excel;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import org.apache.catalina.startup.SetContextPropertiesRule;
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.CreationHelper;
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.ss.util.CellRangeAddress;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestExcel02 {
private Workbook wb = null;
private String path = "F:/test.xls";
//在全部的Test方法執行以前調用 @Before public void createWorkBook(){ this.wb = new HSSFWorkbook(); } //在全部的test方法執行以後調用 @After public void writeWorkBook() throws Exception{ FileOutputStream out = new FileOutputStream(new File(this.path)); this.wb.write(out); out.flush(); out.close(); this.wb.close(); } //測試生成第一個工做簿 @Test public void createFisrtWorkBook(){ this.path = "F:/first.xls"; } //測試生成第一個 單元格 @Test public void createCell(){ this.path = "F:/test_cell.xls"; Sheet sheet = this.wb.createSheet("First"); Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("this is a first"); } //建立一個日期類型的單元格 @Test public void createDateCell(){ Sheet sheet = this.wb.createSheet("日期"); //獲得一個CreationHelper 幫助器 CreationHelper helper = this.wb.getCreationHelper(); Row row = sheet.createRow(0); Cell cell = row.createCell(0); //建立單元格樣式 CellStyle style = this.wb.createCellStyle(); //設置日期的格式話 style.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss")); cell.setCellValue(new Date()); cell.setCellStyle(style); } @Test public void createWorkBookOfUsersInfo(){ String[][] data = new String[][]{ { "123456789", "李四", "lisi@lisi.com", "20", "男" },{ "2", "李四2", "lisi2@lisi.com", "30", "女" },{ "3", "李四3", "lisi3@lisi.com", "22", "男" },{ "4", "李四4", "lisi4@lisi.com", "24", "男" },{ "5", "李四5", "lisi5@lisi.com", "35", "女" } }; String []headers = new String[]{"ID","用戶名","郵箱","年齡","性別"}; //建立工做表 Sheet sheet = this.wb.createSheet("用戶信息"); sheet.setColumnWidth(0, 256*8); //建立title Row title = sheet.createRow(0); //建立單元格樣式 CellStyle tStyle = this.wb.createCellStyle(); //設置水平居中 tStyle.setAlignment(CellStyle.ALIGN_CENTER); tStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); title.setHeight((short)(40*20)); Cell tCell = title.createCell(0); tCell.setCellValue("用戶信息表"); //合併單元格 sheet.addMergedRegion(new CellRangeAddress(0,0,0,headers.length-1)); tCell.setCellStyle(tStyle); //設置表頭 Row header = sheet.createRow(1); for(int i=0;i<headers.length;i++){ Cell cell = header.createCell(i); cell.setCellValue(headers[i]); } for(int i=0;i<data.length;i++){ //建立行 Row row = sheet.createRow(i+2); for(int j=0;j<data[i].length;j++){ //建立單元格 Cell cell = row.createCell(j); //設置數據 cell.setCellValue(data[i][j]); } } this.path = "F:/users.xls"; }}