EXCEL導出總結


下面是我做一個excel導出功能的代碼

前台代碼:

<form action="excelServlet.do" method="post" id="exportForm">
<div style="margin-left: 550px; margin-top: 100px;">
<span>用戶列表</span>
<input type="button" value='導出' onclick="Export()" style='margin-left:50px'/>
</div>
</form>

function Export(){
var form=$("#exportForm");
form.action='excelServlet.do';
form.submit();
}

--------------------------------------------------------------------------------------------------------------------------------

要想連接上servlet的方法,需要再web.xml配置文件中配置一下

<servlet>
<servlet-name>excelServlet</servlet-name>
<servlet-class>servlet.excelServlet</servlet-class><!--這是你所寫servlet類的路徑-->
</servlet>

<servlet-mapping>
<servlet-name>excelServlet</servlet-name>
<url-pattern>/excelServlet.do</url-pattern><!--放置前端文件的路徑再加ervlet-name的內容點上do-->
</servlet-mapping>

-------------------------------------------------------------------------------------------------------------------------------

按這種寫法,就能順利進入servlet中

public class excelServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
super.doGet(request, response);
}

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 清空輸出流
response.reset();

// 輸出流
OutputStream os = response.getOutputStream();

// 文件頭信息
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setCharacterEncoding("UTF-8");

WebApplicationContext context = WebApplicationContextUtils
.getWebApplicationContext(this.getServletContext());
ListServiceImp listServiceImp = (ListServiceImp) context
.getBean("listService");

// 導出主表結果list
List<User> list = new ArrayList<User>();

// 導入至excel文件
PrintExcel excel = new PrintExcel();
// 導出
// String date1 = new java.text.SimpleDateFormat("yyyyMMddHHmmss")
// .format(new java.util.Date());
String fileName = "用戶列表" ;
// 設定輸出文件頭
response.setHeader(
"Content-disposition",
"attachment; filename="
+ new String(fileName.getBytes("gb2312"),
"iso8859-1") + ".xls");
try {
list = listServiceImp.getList();//這個是查詢出一個對象放入list中,這一塊內容可以自己填充,自行補齊,代碼就不貼出來了
} catch (ServiceException e) {
e.printStackTrace();
}
excel.requirePrintExcel(list, os);
}
}

-----------------------------------------------------------------------------------------------------------------------------------------

然后還有一個工具類

public class PrintExcel {
// 資產預投放導出
@SuppressWarnings("deprecation")
public void requirePrintExcel(List<User> list, OutputStream os)
throws IOException {
try {
// 第一步,創建一個webbook,對應一個Excel文件
// HSSFWorkbook wb = new HSSFWorkbook();
Workbook wb = new SXSSFWorkbook();
// 第二步,在webbook中添加一個sheet,對應Excel文件中的sheet
Sheet sheet = wb.createSheet("用戶列表");
// 第三步,在sheet中添加表頭第0行,注意老版本poi對Excel的行數列數有限制short
Row row0 = sheet.createRow((int) 0);

// 定義標題數組
String[] title = {"登錄代號","用戶名稱"};

// 設置標題字體
Font font = wb.createFont();
font.setFontName("宋體");
font.setColor(HSSFColor.BLUE.index);
font.setFontHeightInPoints((short) 14); // 字體大小
font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 加粗
// 設置標題cell樣式
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setFont(font);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER); // 左右居中
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 上下居中
// cellStyle.setLocked(true);
// cellStyle.setWrapText(true); // 自動換行
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
// 設置數據字體
Font font2 = wb.createFont();
font2.setFontName("宋體");
font2.setFontHeightInPoints((short) 9);
// 設置條目cell樣式
DataFormat format = wb.createDataFormat();
// 數字style
CellStyle cellStyleNum = wb.createCellStyle();
cellStyleNum.setFont(font2);
cellStyleNum.setAlignment(CellStyle.ALIGN_RIGHT);
cellStyleNum.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 上下居中
// cellStyleNum.setDataFormat(HSSFDataFormat.getBuiltinFormat("0.00"));//
// 設置單元類型
cellStyleNum.setBorderBottom(CellStyle.BORDER_THIN);
cellStyleNum.setBorderLeft(CellStyle.BORDER_THIN);
cellStyleNum.setBorderRight(CellStyle.BORDER_THIN);
cellStyleNum.setBorderTop(CellStyle.BORDER_THIN);
// 文字style
CellStyle cellStyleText = wb.createCellStyle();
cellStyleText.setFont(font2);
cellStyleText.setAlignment(CellStyle.ALIGN_RIGHT);
cellStyleText.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 上下居中
cellStyleText.setDataFormat(format.getFormat("0.00"));// 設置單元類型
cellStyleText.setBorderBottom(CellStyle.BORDER_THIN);
cellStyleText.setBorderLeft(CellStyle.BORDER_THIN);
cellStyleText.setBorderRight(CellStyle.BORDER_THIN);
cellStyleText.setBorderTop(CellStyle.BORDER_THIN);

// 輸出標題
for (int i = 0; i < title.length; i++) {
Cell cell = row0.createCell((short) i);
cell.setCellType(Cell.CELL_TYPE_STRING);
cell.setCellStyle(cellStyle);
cell.setCellValue(title[i]);
}
// 數據輸出
Cell[] cell1 = new Cell[title.length];
for (int i = 0; i < list.size(); i++) {
Row rowData = sheet.createRow((int) i + 1);
for (int j = 0; j < title.length; j++) {
cell1[j] = rowData.createCell(j);
cell1[j].setCellType(Cell.CELL_TYPE_STRING);
cell1[j].setCellStyle(cellStyleNum);

}
User List = list.get(i);
if(List.getIdNo()!=null&&List.getUserName()!=null){

cell1[0].setCellValue(List.getIdNo());
cell1[1].setCellValue(List.getUserName());
}
}

// 輸出標題
for (int i = 0; i < title.length; i++) {//后面放置在這里是為了解決設置這個自適應寬度特別慢的問題
// 調整列寬度
sheet.autoSizeColumn((short) i);
}
// 第五步,下載文件
// try {
wb.write(os);
// } catch (Exception e) {
// e.printStackTrace();
// os.close();
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
os.close();

}
}
}

------------------------------------------------------------------------------------------------------------------------

到這里,問題就解決了,只要補充上數據,就可以到出到excel表格中


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM