提交POI导出Excel导出功能
This commit is contained in:
@ -8,9 +8,8 @@ import org.apache.poi.ss.util.CellRangeAddress;
|
|||||||
import org.apache.poi.ss.util.CellRangeAddressList;
|
import org.apache.poi.ss.util.CellRangeAddressList;
|
||||||
import org.apache.poi.xssf.usermodel.*;
|
import org.apache.poi.xssf.usermodel.*;
|
||||||
|
|
||||||
import java.io.File;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.FileOutputStream;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -247,4 +246,35 @@ public class ExportConstant {
|
|||||||
sheet.setColumnWidth(i,colList.get(i));//列宽
|
sheet.setColumnWidth(i,colList.get(i));//列宽
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void exportFile(HttpServletResponse response,File file){
|
||||||
|
OutputStream out = null;
|
||||||
|
InputStream is = null;
|
||||||
|
try {
|
||||||
|
response.addHeader("content-disposition", "attachment;filename="
|
||||||
|
+ java.net.URLEncoder.encode(file.getName(), "utf-8"));
|
||||||
|
out = response.getOutputStream();
|
||||||
|
is = new FileInputStream(file);
|
||||||
|
|
||||||
|
byte[] b = new byte[4096];
|
||||||
|
int size = is.read(b);
|
||||||
|
while (size > 0) {
|
||||||
|
out.write(b, 0, size);
|
||||||
|
size = is.read(b);
|
||||||
|
}
|
||||||
|
out.close();
|
||||||
|
is.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if(is!=null)is.close();
|
||||||
|
if(out!=null)out.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import java.util.List;
|
|||||||
public class ExportText {
|
public class ExportText {
|
||||||
public static void main(String[] arge){
|
public static void main(String[] arge){
|
||||||
ExcelTable table = new ExcelTable("测试1");
|
ExcelTable table = new ExcelTable("测试1");
|
||||||
|
table.setFileName("测试11");
|
||||||
List<ExcelTd> list = new ArrayList<>();
|
List<ExcelTd> list = new ArrayList<>();
|
||||||
list.add(new ExcelTd().setTdValue("1111"));
|
list.add(new ExcelTd().setTdValue("1111"));
|
||||||
list.add(new ExcelTd().setTdValue("1222"));
|
list.add(new ExcelTd().setTdValue("1222"));
|
||||||
@ -20,17 +21,19 @@ public class ExportText {
|
|||||||
|
|
||||||
table.add(new ExcelTr().setExcelTdList(list));
|
table.add(new ExcelTr().setExcelTdList(list));
|
||||||
OutputStream os = null;
|
OutputStream os = null;
|
||||||
|
File file = null;
|
||||||
|
InputStream inputStream = null;
|
||||||
try {
|
try {
|
||||||
String path = "D:/恒熠/联通/需求/3.0/评审/common测试.xlsx";
|
String path = "D:/恒熠/联通/需求/3.0/评审/";
|
||||||
File file = ExportConstant.generateExcelByTable(table);
|
file = ExportConstant.generateExcelByTable(table);
|
||||||
InputStream inputStream = new FileInputStream(file);
|
inputStream = new FileInputStream(file);
|
||||||
// 1K的数据缓冲
|
// 1K的数据缓冲
|
||||||
byte[] bs = new byte[1024];
|
byte[] bs = new byte[1024];
|
||||||
// 读取到的数据长度
|
// 读取到的数据长度
|
||||||
int len;
|
int len;
|
||||||
// 输出的文件流保存到本地文件
|
// 输出的文件流保存到本地文件
|
||||||
|
|
||||||
File tempFile = new File(path);
|
File tempFile = new File(path+file.getName());
|
||||||
os = new FileOutputStream(tempFile);
|
os = new FileOutputStream(tempFile);
|
||||||
// 开始读取
|
// 开始读取
|
||||||
while ((len = inputStream.read(bs)) != -1) {
|
while ((len = inputStream.read(bs)) != -1) {
|
||||||
@ -38,6 +41,17 @@ public class ExportText {
|
|||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if(inputStream!=null)inputStream.close();
|
||||||
|
if(os!=null)os.close();
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if(file!=null){
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user