導航:首頁 > 編程語言 > java導出excel換行

java導出excel換行

發布時間:2024-04-05 10:59:29

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>

❷ 如何導出生成excel文件 java

編程中經常需要使用到表格(報表)的處理主要以Excel表格為主。下面給出用java寫入數據到excel表格方法:
1.添加jar文件

java導入導出Excel文件要引入jxl.jar包,最關鍵的是這套API是純Java的,並不依賴Windows系統,即使運行在Linux下,它同樣能夠正確的處理Excel文件。下載地址:http://www.andykhan.com/jexcelapi/

2.jxl對Excel表格的認識

可以參見http://www.cnblogs.com/xudong-bupt/archive/2013/03/19/2969997.html

3.java代碼根據程序中的數據生成上述圖片所示的t.xls文件

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

import java.io.File;
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class Writer_excel{
public static void main(String[] args) {
//標題行
String title[]={"角色","編號","功能名稱","功能描述"};
//內容
String context[][]={{"UC11","設置課程","創建課程"},
{"UC12","設置學生名單","給出與課程關聯的學生名單"},
{"UC21","查看學生名單",""},
{"UC22","查看小組信息","顯示助教所負責的小組列表信息"}
};
//操作執行
try {
//t.xls為要新建的文件名
WritableWorkbook book= Workbook.createWorkbook(new File("t.xls"));
//生成名為「第一頁」的工作表,參數0表示這是第一頁
WritableSheet sheet=book.createSheet("第一頁",0);

//寫入內容
for(int i=0;i<4;i++) //title
sheet.addCell(new Label(i,0,title[i]));
for(int i=0;i<4;i++) //context
{
for(int j=0;j<3;j++)
{
sheet.addCell(new Label(j+1,i+1,context[i][j]));
}
}
sheet.addCell(new Label(0,1,"教師"));
sheet.addCell(new Label(0,3,"助教"));

/*合並單元格.合並既可以是橫向的,也可以是縱向的
*WritableSheet.mergeCells(int m,int n,int p,int q); 表示由(m,n)到(p,q)的單元格組成的矩形區域合並
* */
sheet.mergeCells(0,1,0,2);
sheet.mergeCells(0,3,0,4);

//寫入數據
book.write();
//關閉文件
book.close();
}
catch(Exception e) { }
}

❸ poi 瀵煎嚭excel 鎬庝箞璁劇疆鍚堝苟鐨勫崟鍏冩牸鑷鍔ㄦ崲琛

POI鐢熸垚excel琛ㄦ牸錛屽備綍鍚堝苟鍗曞厓鏍

Java浠g爜
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.Region;

public class ExcelTest {

/**
* @param args
*/
public static void main(String[] args) throws IOException {

try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // 鏍峰紡瀵硅薄

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 鍨傜洿
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 姘村鉤
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);

sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));
HSSFCell ce = row.createCell((short) 0);
ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 涓鏂囧勭悊
ce.setCellValue("欏圭洰\\鏃ユ湡"); // 琛ㄦ牸鐨勭涓琛岀涓鍒楁樉紺虹殑鏁版嵁
ce.setCellStyle(style); // 鏍峰紡錛屽眳涓
int num = 0;
for (int i = 0; i < 9; i++) { // 寰鐜9嬈★紝姣忎竴嬈¢兘瑕佽法鍗曞厓鏍兼樉紺
// 璁$畻浠庨偅涓鍗曞厓鏍艱法鍒伴偅涓鏍
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 0;
celle = 1;
} else {
celln = (i * 2);
celle = (i * 2 + 1);
}
// 鍗曞厓鏍煎悎騫
// 鍥涗釜鍙傛暟鍒嗗埆鏄錛氳搗濮嬭岋紝璧峰嬪垪錛岀粨鏉熻岋紝緇撴潫鍒
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 璺ㄥ崟鍏冩牸鏄劇ず鐨勬暟鎹
cell.setCellStyle(style); // 鏍峰紡
// 涓嶈法鍗曞厓鏍兼樉紺虹殑鏁版嵁錛屽傦細鍒嗕袱琛岋紝涓婁竴琛屽垎鍒涓ゆ牸涓轟竴鏍礆紝涓嬩竴琛屽氨涓轟袱鏍礆紝鈥滄暟閲忊濓紝鈥滈噾棰濃
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("鏁伴噺");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("閲戦");
cell2.setCellStyle(style);
num++;
}

// 鍦ㄥ悗闈㈠姞涓婂悎璁$櫨鍒嗘瘮

// 鍚堣 鍦ㄦ渶鍚庡姞涓婏紝榪樿佽法涓涓鍗曞厓鏍
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)));
HSSFCell cell = row.createCell((short) (2 * num + 1));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("鍚堣");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("鏁伴噺");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("閲戦");
cell2.setCellStyle(style);

// 鐧懼垎姣 鍚屼笂
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
HSSFCell cellb = row.createCell((short) (2 * num + 3));
cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb.setCellValue("鐧懼垎姣");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb1.setCellValue("鏁伴噺");
cellb1.setCellStyle(style);
cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb2.setCellValue("閲戦");
cellb2.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}

}

}

❹ java poi導出excel要雙擊才顯示換行

在開始選項卡下面有個玩意叫自動換行,點一下就好了。

閱讀全文

與java導出excel換行相關的資料

熱點內容
更改android解析度 瀏覽:185
phpstaticfinal 瀏覽:694
成人倫理風月片電影 瀏覽:293
禁播愛情片 瀏覽:21
動漫電影免費版大全 瀏覽:14
java什麼是this 瀏覽:811
拍攝指南by小說製造機txt下載 瀏覽:738
豆瓣pdf 瀏覽:723
春宮妖姬演員表 瀏覽:112
韓國情愛電影在線 瀏覽:812
程序員那麼可愛顧墨吃醋 瀏覽:201
伺服器未測速怎麼辦 瀏覽:15
男主角林晨的都市小說 瀏覽:899
百度文庫系統源碼nodejs 瀏覽:997
電影院正在上映的電影怎麼在家看 瀏覽:61
永輝生活app如何使用手機閃付 瀏覽:178
吸奶的電影 瀏覽:986
對越自衛反擊戰電影大全集免費 瀏覽:565
一起看電影網站源碼 瀏覽:909
阿甘正傳阿甘的英文名 瀏覽:159