導航:首頁 > 編程語言 > poijs導出excel

poijs導出excel

發布時間:2023-04-23 12:01:23

1. java 使用POI導出Excel文件,打開導出文件時報錯

在保護狀態下execl的格式有可能正在被使用,你這邊修改,准確說是線程沖突,一般excel值會作為導出文件的模板,是不會編輯的。你可以在讀的時候判斷execl是否正在被使用。
下面的代碼問題,你可以參考
package com.hwt.glmf.common;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

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.HSSFRichTextString;
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.hssf.util.CellRangeAddress;
import org.apache.poi.hssf.util.HSSFColor;

/**
* 導出Excel公共方法
* @version 1.0
*
* @author wangcp
*
*/
public class ExportExcel extends BaseAction {

//顯示的導出表的標題
private String title;
//導出表的列名
private String[] rowName ;

private List<Object[]> dataList = new ArrayList<Object[]>();

HttpServletResponse response;

//構造方法,傳入要導出的數據
public ExportExcel(String title,String[] rowName,List<Object[]> dataList){
this.dataList = dataList;
this.rowName = rowName;
this.title = title;
}

/*
* 導出數據
* */
public void export() throws Exception{
try{
HSSFWorkbook workbook = new HSSFWorkbook(); // 創建工作簿對象
HSSFSheet sheet = workbook.createSheet(title); // 創建工作表

// 產生表格標題行
HSSFRow rowm = sheet.createRow(0);
HSSFCell cellTiltle = rowm.createCell(0);

//sheet樣式定義【getColumnTopStyle()/getStyle()均為自定義方法 - 在下面 - 可擴展】
HSSFCellStyle columnTopStyle = this.getColumnTopStyle(workbook);//獲取列頭樣式對象
HSSFCellStyle style = this.getStyle(workbook); //單元格樣式對象

sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, (rowName.length-1)));
cellTiltle.setCellStyle(columnTopStyle);
cellTiltle.setCellValue(title);

// 定義所需列數
int columnNum = rowName.length;
HSSFRow rowRowName = sheet.createRow(2); // 在索引2的位置創建行(最頂端的行開始的第二行)

// 將列頭設置到sheet的單元格中
for(int n=0;n<columnNum;n++){
HSSFCell cellRowName = rowRowName.createCell(n); //創建列頭對應個數的單元格
cellRowName.setCellType(HSSFCell.CELL_TYPE_STRING); //設置列頭單元格的數據類型
HSSFRichTextString text = new HSSFRichTextString(rowName[n]);
cellRowName.setCellValue(text); //設置列頭單元格的值
cellRowName.setCellStyle(columnTopStyle); //設置列頭單元格樣式
}

//將查詢出的數據設置到sheet對應的單元格中
for(int i=0;i<dataList.size();i++){

Object[] obj = dataList.get(i);//遍歷每個對象
HSSFRow row = sheet.createRow(i+3);//創建所需的行數

for(int j=0; j<obj.length; j++){
HSSFCell cell = null; //設置單元格的數據類型
if(j == 0){
cell = row.createCell(j,HSSFCell.CELL_TYPE_NUMERIC);
cell.setCellValue(i+1);
}else{
cell = row.createCell(j,HSSFCell.CELL_TYPE_STRING);
if(!"".equals(obj[j]) && obj[j] != null){
cell.setCellValue(obj[j].toString()); //設置單元格的值
}
}
cell.setCellStyle(style); //設置單元格樣式
}
}
//讓列寬隨著導出的列長自動適應
for (int colNum = 0; colNum < columnNum; colNum++) {
int columnWidth = sheet.getColumnWidth(colNum) / 256;
for (int rowNum = 0; rowNum < sheet.getLastRowNum(); rowNum++) {
HSSFRow currentRow;
//當前行未被使用過
if (sheet.getRow(rowNum) == null) {
currentRow = sheet.createRow(rowNum);
} else {
currentRow = sheet.getRow(rowNum);
}
if (currentRow.getCell(colNum) != null) {
HSSFCell currentCell = currentRow.getCell(colNum);
if (currentCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
int length = currentCell.getStringCellValue().getBytes().length;
if (columnWidth < length) {
columnWidth = length;
}
}
}
}
if(colNum == 0){
sheet.setColumnWidth(colNum, (columnWidth-2) * 256);
}else{
sheet.setColumnWidth(colNum, (columnWidth+4) * 256);
}
}

if(workbook !=null){
try
{
String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls";
String headStr = "attachment; filename=\"" + fileName + "\"";
response = getResponse();
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", headStr);
OutputStream out = response.getOutputStream();
workbook.write(out);
}
catch (IOException e)
{
e.printStackTrace();
}
}

}catch(Exception e){
e.printStackTrace();
}

}

/*
* 列頭單元格樣式
*/
public HSSFCellStyle getColumnTopStyle(HSSFWorkbook workbook) {

// 設置字體
HSSFFont font = workbook.createFont();
//設置字體大小
font.setFontHeightInPoints((short)11);
//字體加粗
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體名字
font.setFontName("Courier New");
//設置樣式;
HSSFCellStyle style = workbook.createCellStyle();
//設置底邊框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//設置底邊框顏色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//設置左邊框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//設置左邊框顏色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//設置右邊框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//設置右邊框顏色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//設置頂邊框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//設置頂邊框顏色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在樣式用應用設置的字體;
style.setFont(font);
//設置自動換行;
style.setWrapText(false);
//設置水平對齊的樣式為居中對齊;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//設置垂直對齊的樣式為居中對齊;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}

/*
* 列數據信息單元格樣式
*/
public HSSFCellStyle getStyle(HSSFWorkbook workbook) {
// 設置字體
HSSFFont font = workbook.createFont();
//設置字體大小
//font.setFontHeightInPoints((short)10);
//字體加粗
//font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//設置字體名字
font.setFontName("Courier New");
//設置樣式;
HSSFCellStyle style = workbook.createCellStyle();
//設置底邊框;
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
//設置底邊框顏色;
style.setBottomBorderColor(HSSFColor.BLACK.index);
//設置左邊框;
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
//設置左邊框顏色;
style.setLeftBorderColor(HSSFColor.BLACK.index);
//設置右邊框;
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
//設置右邊框顏色;
style.setRightBorderColor(HSSFColor.BLACK.index);
//設置頂邊框;
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
//設置頂邊框顏色;
style.setTopBorderColor(HSSFColor.BLACK.index);
//在樣式用應用設置的字體;
style.setFont(font);
//設置自動換行;
style.setWrapText(false);
//設置水平對齊的樣式為居中對齊;
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
//設置垂直對齊的樣式為居中對齊;
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

return style;

}
}

2. poi如何通過模板導出excel

共分為六部完成根據模板導出excel操作:
第一步、設置excel模板路徑(setSrcPath)
第二步、設置要生成excel文件路徑(setDesPath)
第三步、設置模板中哪個Sheet列(setSheetName)
第四步、獲取所讀取excel模板的對象(getSheet)
第五步、設置數據(分為6種類型數據:setCellStrValue、setCellDateValue、 setCellDoubleValue、setCellBoolValue、setCellCalendarValue、setCellRichTextStrValue)
第六步、完成導出 (exportToNewFile)

3. poi導出的excel求匯總,

poi導出的excel求匯總的步驟:
1、尋找poi所需要的包,導入到項目中。值得注意的是,不要找poi很老的jar包,很多方法是無效且不好用。建議版本高點。我使用的是poi-3.7版本
2、建立一個導出方法,創建excel表、表的工作空間、單元格如圖所示
3、單元格中存入值,及改變單元格樣式
4、輸出到具體的路徑。

4. 怎麼利用POI 從jsP表格中導出excel文件

java中導出Excel有兩個組件可以使用,一個是jxl,一個是POI,我這里用的是POI。導出是可以在伺服器上生成文件,然後下載,也可以利用輸出流直接在網頁
中彈出對話框提示用戶保存或下載。生成文件的方式會導致伺服器中存在著垃圾文件,實現方式不太優雅,所以這里我採用的是後面直接通過輸出流的方式。

5. 如何使用POI對Excel表進行導入和導出

導入POI的jar包
新建一個項目,在根目錄在新建一個lib文件夾,將jar包復制粘貼到lib文件夾後,右鍵將其添加到項目的build path中,最後的結果如圖所示:

2
編寫java類,新建一個實體類,比如我們要導出資料庫的有關電腦的信息,那麼就建一個Computer實體類,代碼如下:
package com.qiang.poi;
public class Computer {
private int id;
private String name;
private String description;
private double price;
private double credit;
public int getId() {
return id;
}
public Computer(int id, String name, String description, double price,
double credit) {
super();
this.id = id;
this.name = name;
this.description = description;
this.price = price;
this.credit = credit;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getCredit() {
return credit;
}
public void setCredit(double credit) {
this.credit = credit;
}
}
3
新建一個寫入excel的方法,如write2excel,參數可以後面邊寫邊決定(站在一個不熟悉POI的角度)
public static void write2Excel(){}
4
創建操作Excel的HSSFWorkbook對象
HSSFWorkbook excel= new HSSFWorkbook();
創建HSSFSheet對象
Excel中的一個sheet(工作表)對應著java中的一個HSSFSheet對象,利用HSSFWorkbook對象可以創建一個HSSFSheet對象
如:創建一個sheet名為computer的excel
HSSFSheet sheet = excel.createSheet("computer");
創建第一行標題信息的HSSFRow對象
我們都知道excel是表格,即由一行一行組成的,那麼這一行在java類中就是一個HSSFRow對象,我們通過HSSFSheet對象就可以創建HSSFRow對象
如:創建表格中的第一行(我們常用來做標題的行) HSSFRow firstRow = sheet.createRow(0); 注意下標從0開始
創建標題行中的HSSFCell數組
當然,excel中每一行是由若干個單元格,我們常稱為cell,它對應著java中的HSSFCell對象
如:創建5個單元格 HSSFCell cells[] = new HSSFCell[5];
//假設我們一行有五列數據
創建標題數據,並通過HSSFCell對象的setCellValue()方法對每個單元格進行賦值
既然單元格都准備好了,那最後是不是該填充數據了呀。對的,沒錯。填充數據之前,得把數據准備好吧,
數據:String[] titles = new String[]{"id","name","description","price","credit"};
插入一句話: 在這個時代,能讓機器做的,盡量不讓人來做,記住這句話。
好的,繼續。現在就通過for循環來填充第一行標題的數據
for (int i = 0; i < 5; i++) {
cells[0] = firstRow.createCell(i);
cells[0].setCellValue(titles[i]);
}
數據分析
第一行標題欄創建完畢後,就准備填充我們要寫入的數據吧,在java中,面向對象給我們帶來的好處在這里正好體現了,沒錯
把要填寫的數據封裝在對象中,即一行就是一個對象,n行就是一個對象列表嘛,好的,走起。
創建對象Computer,私有屬性id,name,description,price,credit,以及各屬性的setter和getter方法,如步驟二所示。
假設我們要寫入excel中的數據從資料庫查詢出來的,最後就生成了一個List<Computer>對象computers
數據寫入
具體數據有了,又該讓機器幫我們幹活了,向excel中寫入數據。
for (int i = 0; i < computers.size(); i++) {
HSSFRow row = sheet.createRow(i + 1);
Computer computer = computers.get(i);
HSSFCell cell = row.createCell(0);
cell.setCellValue(computer.getId());
cell = row.createCell(1);
cell.setCellValue(computer.getName());
cell = row.createCell(2);
cell.setCellValue(computer.getDescription());
cell = row.createCell(3);
cell.setCellValue(computer.getPrice());
cell = row.createCell(4);
cell.setCellValue(computer.getCredit());
}
將數據真正的寫入excel文件中
做到這里,數據都寫好了,最後就是把HSSFWorkbook對象excel寫入文件中了。
OutputStream out = null;
try {
out = new FileOutputStream(file);
excel.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("數據已經寫入excel"); //溫馨提示
看看我的main方法吧
public static void main(String[] args) throws IOException {
File file = new File("test1.xls");
if(!file.exists()){
file.createNewFile();
}
List<Computer> computers = new ArrayList<Computer>();
computers.add(new Computer(1,"宏碁","筆記本電腦",3333,9.0));
computers.add(new Computer(2,"蘋果","筆記本電腦,一體機",8888,9.6));
computers.add(new Computer(3,"聯想","筆記本電腦,台式機",4444,9.3));
computers.add(new Computer(4, "華碩", "筆記本電腦,平板電腦",3555,8.6));
computers.add(new Computer(5, "註解", "以上價格均為捏造,如有雷同,純屬巧合", 1.0, 9.9));
write2excel(computers, file);
}
工程目錄及執行main方法後的test1.xls數據展示

源碼分享,computer就不貼了
package com.qiang.poi;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
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;
public class ReadExcel {
public static void main(String[] args) throws IOException {
File file = new File("test1.xls");
if(!file.exists()){
file.createNewFile();
}
List<Computer> computers = new ArrayList<Computer>();
computers.add(new Computer(1,"宏碁","筆記本電腦",3333,9.0));
computers.add(new Computer(2,"蘋果","筆記本電腦,一體機",8888,9.6));
computers.add(new Computer(3,"聯想","筆記本電腦,台式機",4444,9.3));
computers.add(new Computer(4, "華碩", "筆記本電腦,平板電腦",3555,8.6));
computers.add(new Computer(5, "註解", "以上價格均為捏造,如有雷同,純屬巧合", 1.0, 9.9));
write2excel(computers, file);
}

public static void write2excel(List<Computer> computers,File file) {
HSSFWorkbook excel = new HSSFWorkbook();
HSSFSheet sheet = excel.createSheet("computer");
HSSFRow firstRow = sheet.createRow(0);
HSSFCell cells[] = new HSSFCell[5];
String[] titles = new String[] { "id", "name", "description", "price",
"credit" };
for (int i = 0; i < 5; i++) {
cells[0] = firstRow.createCell(i);
cells[0].setCellValue(titles[i]);
}
for (int i = 0; i < computers.size(); i++) {
HSSFRow row = sheet.createRow(i + 1);
Computer computer = computers.get(i);
HSSFCell cell = row.createCell(0);
cell.setCellValue(computer.getId());
cell = row.createCell(1);
cell.setCellValue(computer.getName());
cell = row.createCell(2);
cell.setCellValue(computer.getDescription());
cell = row.createCell(3);
cell.setCellValue(computer.getPrice());
cell = row.createCell(4);
cell.setCellValue(computer.getCredit());
}
OutputStream out = null;
try {
out = new FileOutputStream(file);
excel.write(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

6. java導出數據到excel的幾種方法的比較

Excel的兩種導出入門方法(JAVA與JS)

最近在做一個小項目作為練手,其中使用到了導出到Excel表格,一開始做的是使用JAVA的POI導出的,但因為我的數據是爬蟲爬出來的,數據暫時並不保存在資料庫或後台,所以直接顯示在HTML的table,需要下載時又要將數據傳回後台然後生成Excel文件,最後再從伺服器下載到本地,過程幾度經過網路傳輸,感覺比較耗時與浪費性能,於是想著在HTML中的Table直接導到Excel中節約資源

JAVA導出EXCEL(.xls)

導出Excel用的插件是apache的poi.jar,maven地址如下

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version></dependency>

1. 簡單應用

先來個簡化無樣式的Excel導出,由於我的數據存在JSON中,所以形參是JSONArray,朋友們根據自己的實際數據類型(Map,List,Set等)傳入即可 ,代碼如下

/**
* 創建excel並填入數據
* @author LiQuanhui
* @date 2017年11月24日 下午5:25:13
* @param head 數據頭
* @param body 主體數據
* @return HSSFWorkbook
*/
public static HSSFWorkbook expExcel(JSONArray head, JSONArray body) { //創建一個excel工作簿
HSSFWorkbook workbook = new HSSFWorkbook(); //創建一個sheet工作表
HSSFSheet sheet = workbook.createSheet("學生信息");
//創建第0行表頭,再在這行里在創建單元格,並賦值
HSSFRow row = sheet.createRow(0);
HSSFCell cell = null; for (int i = 0; i < head.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(head.getString(i));//設置值
}
//將主體數據填入Excel中
for (int i = 0, isize = body.size(); i < isize; i++) {
row = sheet.createRow(i + 1);
JSONArray stuInfo = body.getJSONArray(i); for (int j = 0, jsize = stuInfo.size(); j < jsize; j++) {
cell = row.createCell(j);
cell.setCellValue(stuInfo.getString(j));//設置值
}
} return workbook;
}

創建好Excel對象並填好值後(就是得到workbook),就是將這個對象以文件流的形式輸出到本地上去,代碼如下

/**
* 文件輸出
* @author LiQuanhui
* @date 2017年11月24日 下午5:26:23
* @param workbook 填充好的workbook
* @param path 存放的位置
*/
public static void outFile(HSSFWorkbook workbook,String path) {
OutputStream os=null; try {
os = new FileOutputStream(new File(path));
workbook.write(os);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

至此Excel的導出其實已經做完了。

2. 添加樣式後導出

但通常這並不能滿足我們的需求,因為通常是需要設置Excel的一些樣式的,如字體、居中等等,設置單元格樣式主要用到這個類(HSSFCellStyle)

HSSFCellStyle cellStyle = workbook.createCellStyle();

現在說說HSSFCellStyle都能幹些什麼

HSSFCellStyle cellStyle = workbook.createCellStyle();//創建單元格樣式對象1.設置字體
HSSFFont font = workbook.createFont(); //font.setFontHeight((short)12);//這個設置字體會很大
font.setFontHeightInPoints((short)12);//這才是我們平常在Excel設置字體的值
font.setFontName("黑體");//字體:宋體、華文行楷等等
cellStyle.setFont(font);//將該字體設置進去2.設置對齊方式
cellStyle.setAlignment(horizontalAlignment);//horizontalAlignment參考下面給出的參數
//以下是最常用的三種對齊分別是居中,居左,居右,其餘的寫代碼的時候按提示工具查看即可
HorizontalAlignment.CENTER
HorizontalAlignment.LEFT
HorizontalAlignment.RIGHT3.設置邊框
cellStyle.setBorderBottom(border); // 下邊框
cellStyle.setBorderLeft(border);// 左邊框
cellStyle.setBorderTop(border);// 上邊框
cellStyle.setBorderRight(border);// 右邊框
//border的常用參數如下
BorderStyle.NONE 無邊框
BorderStyle.THIN 細邊框
BorderStyle.MEDIUM 中等粗邊框
BorderStyle.THICK 粗邊框//其餘的我也描述不清是什麼形狀,有興趣的到時可以直接測試

在經過一系列的添加樣式之後,最後就會給單元格設置樣式

cell.setCellStyle(cellStyle);

3. 自動調整列寬

sheet.autoSizeColumn(i);//i為第幾列,需要全文都單元格居中的話,需要遍歷所有的列數

4. 完整的案例

public class ExcelUtils { /**
* 創建excel並填入數據
* @author LiQuanhui
* @date 2017年11月24日 下午5:25:13
* @param head 數據頭
* @param body 主體數據
* @return HSSFWorkbook
*/
public static HSSFWorkbook expExcel(JSONArray head, JSONArray body) {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("學生信息");

HSSFRow row = sheet.createRow(0);
HSSFCell cell = null;

HSSFCellStyle cellStyle = workbook.createCellStyle();
setBorderStyle(cellStyle, BorderStyle.THIN);
cellStyle.setFont(setFontStyle(workbook, "黑體", (short) 14));
cellStyle.setAlignment(HorizontalAlignment.CENTER);
for (int i = 0; i < head.size(); i++) {
cell = row.createCell(i);
cell.setCellValue(head.getString(i));
cell.setCellStyle(cellStyle);
}

HSSFCellStyle cellStyle2 = workbook.createCellStyle();
setBorderStyle(cellStyle2, BorderStyle.THIN);
cellStyle2.setFont(setFontStyle(workbook, "宋體", (short) 12));
cellStyle2.setAlignment(HorizontalAlignment.CENTER); for (int i = 0, isize = body.size(); i < isize; i++) {
row = sheet.createRow(i + 1);
JSONArray stuInfo = body.getJSONArray(i); for (int j = 0, jsize = stuInfo.size(); j < jsize; j++) {
cell = row.createCell(j);
cell.setCellValue(stuInfo.getString(j));
cell.setCellStyle(cellStyle2);
}
} for (int i = 0, isize = head.size(); i < isize; i++) {
sheet.autoSizeColumn(i);
} return workbook;
} /**
* 文件輸出
* @author LiQuanhui
* @date 2017年11月24日 下午5:26:23
* @param workbook 填充好的workbook
* @param path 存放的位置
*/
public static void outFile(HSSFWorkbook workbook,String path) {
OutputStream os=null; try {
os = new FileOutputStream(new File(path));
workbook.write(os);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 設置字體樣式
* @author LiQuanhui
* @date 2017年11月24日 下午3:27:03
* @param workbook 工作簿
* @param name 字體類型
* @param height 字體大小
* @return HSSFFont
*/
private static HSSFFont setFontStyle(HSSFWorkbook workbook, String name, short height) {
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints(height);
font.setFontName(name); return font;
} /**
* 設置單元格樣式
* @author LiQuanhui
* @date 2017年11月24日 下午3:26:24
* @param workbook 工作簿
* @param border border樣式
*/
private static void setBorderStyle(HSSFCellStyle cellStyle, BorderStyle border) {
cellStyle.setBorderBottom(border); // 下邊框
cellStyle.setBorderLeft(border);// 左邊框
cellStyle.setBorderTop(border);// 上邊框
cellStyle.setBorderRight(border);// 右邊框
}
}

POI的功能其實還是很強大的,這里只介紹了Excel的一丁點皮毛給入門的查看,如果想對Excel進行更多的設置可以查看下面的這篇文章,有著大量的使用說明。
空谷幽瀾的POI使用詳解

JS導出EXCEL(.xls)

java的Excel導出提供了強大的功能,但也對伺服器造成了一定資源消耗,若能使用客戶端的資源那真是太好了

1. 簡單應用

JS的導出Excel非常簡單,只需要引用Jquery和tableExport.js並設置一個屬性即可

<script src="<%=basePath%>/static/js/tableExport.js" type="text/javascript"></script><script type="text/javascript">
function exportExcelWithJS(){ //獲取要導出Excel的表格對象並設置tableExport方法,設置導出類型type為excel
$('#tableId').tableExport({ type:'excel'
});
}</script><button class="btn btn-primary" type="button" style="float: right;" onclick="exportExcelWithJS()">下載本表格</button>

JS的導出就完成了,是不是特別簡單

2. 進階應用

但上面僅僅是個簡單的全表無樣式的導出
這tableExport.js還有一些其他功能,忽略行,忽略列,設置樣式等,屬性如下

<script type="text/javascript">
function exportExcelWithJS(){ //獲取要導出Excel的表格對象並設置tableExport方法,設置導出類型type為excel
$('#tableId').tableExport({ type:'excel',//導出為excel
fileName:'2017工資表',//文件名
worksheetName:'11月工資',//sheet表的名字
ignoreColumn:[0,1,2],//忽略的列,從0開始算
ignoreRow:[2,4,5],//忽略的行,從0開始算
excelstyles:['text-align']//使用樣式,不用填值只寫屬性,值讀取的是html中的
});
}</script>

7. Java 利用poi 導出excel表格如何在導出時自由選擇路徑

導出時自由選擇路徑的代碼如下:

1、後台輸出Excel文件代碼:


OutputStream output = response.getOutputStream();


response.reset();


response.setHeader("Content-disposition", "attachment; filename=" + path);


response.setContentType("Content-Type:application/vnd.ms-excel ");


wb.write(output);


output.close();

2、前端代碼:

window.open("getExcelList","_blank");

8. 關於java poi 導出Excel如何導出的問題

簡單的辦法是,先做一份excel,這只好每一列的格式。把這個excel放到項目中,每次導出都是用這個excel當作模板復制一份即可。

9. poi導出Excel問題

你的方法應該是沒有問題,404錯誤只是你的跳轉沒響應,估計跳轉頁面路徑錯了。沒有你的配置文件不好說

10. java poi導出excel

用spire.xls.jar也可以導出excel,代碼更簡單

  1. import com.spire.xls.ExcelVersion;

  2. import com.spire.xls.Workbook;

  3. import com.spire.xls.Worksheet;


  4. public class InsertArray {


  5. public static void main(String[] args) {


  6. //創建Workbook對象

  7. Workbook wb = new Workbook();

  8. //獲取第一張工作表

  9. Worksheet sheet = wb.getWorksheets().get(0);

  10. //定義一維數據

  11. String[] oneDimensionalArray = new String[]{"蘋果", "梨子", "葡萄", "香蕉"};

  12. //將數組從指定單個格開始寫入工作表,true表示縱向寫入,設置為false為橫向寫入

  13. sheet.insertArray(oneDimensionalArray, 1, 1, true);

  14. //定義二維數組

  15. String[][] twoDimensionalArray = new String[][]{

  16. {"姓名", "年齡", "性別", "學歷"},

  17. {"小張", "25", "男", "本科"},

  18. {"小王", "24", "男", "本科"},

  19. {"小李", "26", "女", "本科"}

  20. };

  21. //從指定單元格開始寫入二維數組到工作表

  22. sheet.insertArray(twoDimensionalArray, 1, 3);

  23. //保存文檔

  24. wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);

  25. }

  26. }

閱讀全文

與poijs導出excel相關的資料

熱點內容
artlantis渲染器教程 瀏覽:679
360系統文件可以清理嗎 瀏覽:256
extjsform樣式 瀏覽:513
電信貓怎麼設置wifi密碼 瀏覽:785
p190文件用什麼打開 瀏覽:252
怎麼修改ps簽署文件 瀏覽:847
怎麼找到編程貓作品文件 瀏覽:647
鐵路局的網站是多少 瀏覽:194
微信雙號 瀏覽:926
招標文件中的凈值是什麼意思 瀏覽:675
有哪些app能借出5000 瀏覽:250
編程語言哪個發展好 瀏覽:974
刪除xp密碼 瀏覽:974
手機怎麼在word製作作業文件 瀏覽:489
工行銀行卡安全升級 瀏覽:807
桌面放的文件找不到 瀏覽:922
買學生票用什麼app 瀏覽:590
共建共享網路平台 瀏覽:39
js傳值到超鏈接裡面 瀏覽:608
編程中的w和h是什麼 瀏覽:313

友情鏈接