Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 477b010

Browse files
update
1 parent 71fcddf commit 477b010

File tree

3 files changed

+81
-208
lines changed

3 files changed

+81
-208
lines changed
Lines changed: 56 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,169 +1,68 @@
11
package cn.netbuffer.jfinal_bootstrap_table.util;
22

3+
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
4+
import org.apache.poi.ss.usermodel.*;
5+
36
import java.io.FileOutputStream;
47
import java.io.IOException;
5-
import java.util.HashMap;
68
import java.util.Iterator;
79
import java.util.List;
810
import java.util.Map;
911
import java.util.Set;
1012

11-
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
12-
import org.apache.poi.ss.usermodel.Cell;
13-
import org.apache.poi.ss.usermodel.CellStyle;
14-
import org.apache.poi.ss.usermodel.CreationHelper;
15-
import org.apache.poi.ss.usermodel.Font;
16-
import org.apache.poi.ss.usermodel.Hyperlink;
17-
import org.apache.poi.ss.usermodel.IndexedColors;
18-
import org.apache.poi.ss.usermodel.Row;
19-
import org.apache.poi.ss.usermodel.Sheet;
20-
import org.apache.poi.ss.usermodel.Workbook;
21-
import org.apache.poi.ss.util.CellUtil;
22-
2313
public class POIExcelUtil {
24-
public static void exec(String path) {
25-
Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
26-
CreationHelper createHelper = wb.getCreationHelper();
27-
Sheet sheet = wb.createSheet("用户信息");
28-
// 冻结该行,使其无法移动
29-
sheet.createFreezePane(0, 1, 0, 1);
30-
// Header header = sheet1.getHeader();
31-
// header.setCenter("Center Header");
32-
// header.setLeft("Left Header");
33-
// header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
34-
// HSSFHeader.fontSize((short) 16) +
35-
// "Right w/ Stencil-Normal Italic font and size 16");
36-
Row row = sheet.createRow((short) 0);
37-
row.setHeightInPoints(30);
38-
Cell cell = row.createCell(0);
39-
CellStyle cellStyle = wb.createCellStyle();
40-
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
41-
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
42-
cell.setCellValue("用户昵称");
43-
cell.setCellStyle(cellStyle);
44-
row.createCell(1).setCellValue("用户性别");
45-
46-
CellStyle hlink_style = wb.createCellStyle();
47-
Font hlink_font = wb.createFont();
48-
hlink_font.setUnderline(Font.U_SINGLE);
49-
hlink_font.setColor(IndexedColors.BLUE.getIndex());
50-
hlink_style.setFont(hlink_font);
51-
hlink_style.setAlignment(CellStyle.ALIGN_CENTER);
52-
hlink_style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
53-
Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
54-
link.setAddress("http://poi.apache.org/");
55-
cell.setHyperlink(link);
56-
CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_TOP, CellStyle.BORDER_MEDIUM);
57-
CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_MEDIUM);
58-
CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_LEFT, CellStyle.BORDER_MEDIUM);
59-
CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_RIGHT, CellStyle.BORDER_MEDIUM);
60-
CellUtil.setCellStyleProperty(cell, wb, CellUtil.TOP_BORDER_COLOR,IndexedColors.RED.getIndex());
61-
CellUtil.setCellStyleProperty(cell, wb, CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex());
62-
CellUtil.setCellStyleProperty(cell, wb, CellUtil.LEFT_BORDER_COLOR,IndexedColors.RED.getIndex());
63-
CellUtil.setCellStyleProperty(cell, wb, CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex());
64-
row.createCell(2).setCellValue(
65-
createHelper.createRichTextString("This is a string用户年龄"));
66-
row.createCell(3).setCellValue("用户手机");
67-
row.createCell(4).setCellValue("收货地址");
68-
row.createCell(5).setCellValue("注册时间");
69-
sheet.autoSizeColumn(0); // adjust width of the first column
70-
sheet.autoSizeColumn(1);
71-
sheet.autoSizeColumn(2);
72-
sheet.autoSizeColumn(3);
73-
sheet.autoSizeColumn(4);
74-
75-
FileOutputStream fileOut;
76-
try {
77-
fileOut = new FileOutputStream(path);
78-
wb.write(fileOut);
79-
fileOut.close();
80-
} catch (IOException e) {
81-
e.printStackTrace();
82-
}
83-
84-
}
85-
86-
public static void export(List<String> titles,List<Map<String, Object>> datas,String path) {
87-
Workbook wb = new HSSFWorkbook();
88-
CreationHelper createHelper = wb.getCreationHelper();
89-
Sheet sheet = wb.createSheet(titles.get(0));
90-
// 冻结该行,使其无法移动
91-
sheet.createFreezePane(0, 1, 0, 1);
92-
// Header header = sheet1.getHeader();
93-
// header.setCenter("Center Header");
94-
// header.setLeft("Left Header");
95-
// header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
96-
// HSSFHeader.fontSize((short) 16) +
97-
// "Right w/ Stencil-Normal Italic font and size 16");
98-
Row row = sheet.createRow((short) 0);
99-
row.setHeightInPoints(30);
100-
int titleCount=titles.size();
101-
Font titleFont=wb.createFont();
102-
titleFont.setBold(true);
103-
titleFont.setColor(IndexedColors.AQUA.getIndex());
104-
CellStyle cellStyle = wb.createCellStyle();
105-
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
106-
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
107-
cellStyle.setFont(titleFont);
108-
for(int i=0;i<titleCount;i++){
109-
Cell cell = row.createCell(i);
110-
cell.setCellValue(titles.get(i));
111-
cell.setCellStyle(cellStyle);
112-
}
113-
CellStyle contentStyle = wb.createCellStyle();
114-
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
115-
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
116-
int dataCount=datas.size();
117-
for(int i=1;i<dataCount+1;i++){
118-
Row rowIndex = sheet.createRow((short) i);
119-
Map<String, Object> data=datas.get(i-1);
120-
Set<String> dataSet=data.keySet();
121-
Iterator<String> d=dataSet.iterator();
122-
int k=0;
123-
while (d.hasNext()) {
124-
Cell cell = rowIndex.createCell(k);
125-
cell.setCellStyle(contentStyle);
126-
String key = (String) d.next();
127-
cell.setCellValue(data.get(key)==null?"":data.get(key).toString());
128-
k++;
129-
}
130-
}
131-
// CellStyle hlink_style = wb.createCellStyle();
132-
// Font hlink_font = wb.createFont();
133-
// hlink_font.setUnderline(Font.U_SINGLE);
134-
// hlink_font.setColor(IndexedColors.BLUE.getIndex());
135-
// hlink_style.setFont(hlink_font);
136-
// hlink_style.setAlignment(CellStyle.ALIGN_CENTER);
137-
// hlink_style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
138-
// Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
139-
// link.setAddress("http://poi.apache.org/");
140-
// cell.setHyperlink(link);
141-
// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_TOP, CellStyle.BORDER_MEDIUM);
142-
// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_BOTTOM, CellStyle.BORDER_MEDIUM);
143-
// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_LEFT, CellStyle.BORDER_MEDIUM);
144-
// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BORDER_RIGHT, CellStyle.BORDER_MEDIUM);
145-
// CellUtil.setCellStyleProperty(cell, wb, CellUtil.TOP_BORDER_COLOR,IndexedColors.RED.getIndex());
146-
// CellUtil.setCellStyleProperty(cell, wb, CellUtil.BOTTOM_BORDER_COLOR, IndexedColors.RED.getIndex());
147-
// CellUtil.setCellStyleProperty(cell, wb, CellUtil.LEFT_BORDER_COLOR,IndexedColors.RED.getIndex());
148-
// CellUtil.setCellStyleProperty(cell, wb, CellUtil.RIGHT_BORDER_COLOR, IndexedColors.RED.getIndex());
149-
// row.createCell(2).setCellValue(
150-
// createHelper.createRichTextString("This is a string用户年龄"));
151-
// row.createCell(3).setCellValue("用户手机");
152-
// row.createCell(4).setCellValue("收货地址");
153-
// row.createCell(5).setCellValue("注册时间");
154-
// sheet.autoSizeColumn(0); // adjust width of the first column
155-
// sheet.autoSizeColumn(1);
156-
// sheet.autoSizeColumn(2);
157-
// sheet.autoSizeColumn(3);
158-
// sheet.autoSizeColumn(4);
159-
FileOutputStream fileOut;
160-
try {
161-
fileOut = new FileOutputStream(path);
162-
wb.write(fileOut);
163-
fileOut.close();
164-
} catch (IOException e) {
165-
e.printStackTrace();
166-
}
16714

168-
}
15+
public static void export(List<String> titles, List<Map<String, Object>> datas, String path) {
16+
Workbook wb = new HSSFWorkbook();
17+
Sheet sheet = wb.createSheet(titles.get(0));
18+
// 冻结该行,使其无法移动
19+
sheet.createFreezePane(0, 1, 0, 1);
20+
// Header header = sheet.getHeader();
21+
// header.setCenter("Center Header");
22+
// header.setLeft("Left Header");
23+
// header.setRight(HSSFHeader.font("Stencil-Normal", "Italic");
24+
// HSSFHeader.fontSize((short) 16);
25+
// "Right w/ Stencil-Normal Italic font and size 16");
26+
Row row = sheet.createRow((short) 0);
27+
row.setHeightInPoints(30);
28+
int titleCount = titles.size();
29+
Font titleFont = wb.createFont();
30+
titleFont.setBold(true);
31+
titleFont.setColor(IndexedColors.AQUA.getIndex());
32+
CellStyle cellStyle = wb.createCellStyle();
33+
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
34+
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
35+
cellStyle.setFont(titleFont);
36+
for (int i = 0; i < titleCount; i++) {
37+
Cell cell = row.createCell(i);
38+
cell.setCellValue(titles.get(i));
39+
cell.setCellStyle(cellStyle);
40+
}
41+
CellStyle contentStyle = wb.createCellStyle();
42+
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
43+
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
44+
int dataCount = datas.size();
45+
for (int i = 1; i < dataCount + 1; i++) {
46+
Row rowIndex = sheet.createRow((short) i);
47+
Map<String, Object> data = datas.get(i - 1);
48+
Set<String> dataSet = data.keySet();
49+
Iterator<String> d = dataSet.iterator();
50+
int k = 0;
51+
while (d.hasNext()) {
52+
Cell cell = rowIndex.createCell(k);
53+
cell.setCellStyle(contentStyle);
54+
String key = d.next();
55+
cell.setCellValue(data.get(key) == null ? "" : data.get(key).toString());
56+
k++;
57+
}
58+
}
59+
FileOutputStream fileOut;
60+
try {
61+
fileOut = new FileOutputStream(path);
62+
wb.write(fileOut);
63+
fileOut.close();
64+
} catch (IOException e) {
65+
e.printStackTrace();
66+
}
67+
}
16968
}

‎src/main/webapp/manage.html‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@
7171
</button>
7272
</div>
7373
</div> -->
74-
<br/><br/><br/>
74+
<divstyle="margin-top:55px;"></div>
7575
<button type="button" class="btn btn-default"
7676
onclick="exporyAll();">
7777
<span class="glyphicon glyphicon-arrow-down"></span> 导出历史数据到excel
7878
</button>
79+
<button type="button" class="btn btn-default"
80+
onclick="import_from_xls();">
81+
<span class="glyphicon glyphicon-arrow-up"></span> 导入用户
82+
</button>
7983
<table id="dtb" data-toggle="table" data-url="user/userlist"
8084
class="table table-hover" data-show-columns="true" data-search="true"
8185
data-show-refresh="true" data-show-toggle="true"
Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,32 @@
11
package cn.netbuffer.jfinal_bootstrap_table.util;
22

3+
import org.junit.Test;
4+
35
import java.io.File;
4-
import java.io.IOException;
5-
import java.net.URL;
66
import java.util.ArrayList;
77
import java.util.LinkedHashMap;
88
import java.util.List;
99
import java.util.Map;
1010

11-
import org.junit.Test;
12-
1311
public class TestPOI {
1412

15-
@Test
16-
public void tt() throws IOException {
17-
// File directory = new File("");// 参数为空
18-
// String projectpath=directory.getAbsolutePath();
19-
// POIExcelUtil.exec(projectpath+File.separator+"aa.xls");
20-
// List<String> titles=new ArrayList<String>(){};
21-
// titles.add("昵称");
22-
// titles.add("姓名");
23-
// titles.add("性别");
24-
// List<Map<String, Object>> datas=new ArrayList<Map<String, Object>>();
25-
// for(int i=0;i<4;i++){
26-
// //保障顺序
27-
// Map<String, Object> mp=new LinkedHashMap<String, Object>();
28-
// mp.put("n","tt"+i);
29-
// mp.put("x","行ing"+i);
30-
// mp.put("b","男"+i);
31-
// datas.add(mp);
32-
// }
33-
// POIExcelUtil.export(titles, datas, projectpath+File.separator+"test.xls");
34-
}
35-
36-
public void sysoutpath() throws IOException {
37-
// 第一种:获取类加载的根路径
38-
File f = new File(this.getClass().getResource("/").getPath());
39-
System.out.println(f);
40-
// 获取当前类的所在工程路径; 如果不加"/" 获取当前类的加载目录
41-
File f2 = new File(this.getClass().getResource("").getPath());
42-
System.out.println(f2);
43-
44-
// 第二种获取项目路径
45-
File directory = new File("");// 参数为空
46-
System.out.println(directory.getAbsolutePath());
47-
String courseFile = directory.getCanonicalPath();
48-
System.out.println(courseFile);
49-
50-
// 第三种
51-
URL xmlpath = this.getClass().getClassLoader().getResource("");
52-
System.out.println(xmlpath);
13+
@Test
14+
public void testExportFile() {
15+
String dir = System.getProperty("user.dir");
16+
List<String> titles = new ArrayList<>();
17+
titles.add("昵称");
18+
titles.add("姓名");
19+
titles.add("性别");
20+
List<Map<String, Object>> datas = new ArrayList<Map<String, Object>>();
21+
for (int i = 0; i < 4; i++) {
22+
//保障顺序
23+
Map<String, Object> mp = new LinkedHashMap<String, Object>();
24+
mp.put("n", "tt" + i);
25+
mp.put("x", "行ing" + i);
26+
mp.put("b", "男" + i);
27+
datas.add(mp);
28+
}
29+
POIExcelUtil.export(titles, datas, dir + File.separator + "test.xls");
30+
}
5331

54-
// 第四种
55-
System.out.println(System.getProperty("user.dir"));
56-
/*
57-
* 获取当前工程路径
58-
*/
59-
// 第五种: 获取所有的类路径 包括jar包的路径
60-
System.out.println(System.getProperty("java.class.path"));
61-
}
6232
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /