众所周知,mysql技术让我们非常便捷的操作各类数据,但mysql制作成实体的表最方便的方式还是使用专门的表格操作软件,所以我们需要将mysql数据库中的数据导入到excel中。
java中mysql导入excel表格所需要的 UsePioOutExcel 工具代码
package utils;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class UsePioOutExcel {
/**
* 导出excel通用类
* @param list 要导出的数据
* @param columnSize 列数
* @param response response对象
* @param sheetName sheet的名字
* @param fileName 文件名
* @throws IOException
*/
public static void writeExcel (List list,int columnSize,HttpServletResponse response,String sheetName,String fileName,String[] cellFormatType) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(); //获取一个sheetnew String( sheetName.getBytes("GBK"),"ISO8859-1")
wb.setSheetName(0, sheetName);
HSSFHeader header = sheet.getHeader();
header.setCenter("Center Header");
HSSFRow row=null;
for(int rowId=0;rowId<list.size();rowId++){
String[] st=(String[])list.get(rowId);
row = sheet.createRow(rowId); //创建行
for(int columnId=0; columnId < columnSize; columnId++){
HSSFCell cell = row.createCell((short)columnId); //创建格
//cell.setEncoding("UTF-16"); //设置单元格的字符编码格式
if(cellFormatType==null || cellFormatType.length==0){ //如果cellFormatType数组不为空
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(st[columnId]);
}else{
if(!cellFormatType[columnId].equals("t") && rowId!=0){
cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
//如果数值不为空
if(st[columnId]!=null){
cell.setCellValue(Double.parseDouble(st[columnId]));
}else{
cell.setCellValue(st[columnId]);
}
}else{
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(st[columnId]);
}
}
}
}
/**
* 弹框,让用户选择保存路径
*/
response.reset();
response.setHeader( "Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("GBK"), "ISO8859-1"));
wb.write(response.getOutputStream());
response.getOutputStream().close();
}
}
servelt文件中输出excel代码
protected void carInfoOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Map<String, String>> list=cs.selectAllCarsInfo();
String[] titles = { "编号", "商品名称","地址"};//表头//这是excel中要显示的列,就是sql查出的字段列
List excelList = new ArrayList();
excelList.add(titles);//先将表头存在集合中
for (int rowId = 0; rowId < list.size(); rowId++) {//rowid代表行数
Map order = list.get(rowId);
String[] rowContents = { order.get("id").toString(),//这个你应该懂得 获取值
(String) order.get("gname"),
(String) order.get("pic")
};
excelList.add(rowContents);//将数据库里面的数据一个一个的存放在集合中
}
String[] cellFormatType = {"t","t","t"};//这个是有多少列明 就有多少个t//记住就行
UsePioOutExcel.writeExcel(excelList, 3, response, "汽车信息表", "汽车信息表.xls", cellFormatType);
}
将从数据库里面取出来的值放到List<Map<String,String>>集合中
List<Map<String, String>> list=cs.selectAllCarsInfo();
writeExcel (List list,int columnSize,HttpServletResponse response,String sheetName,String fileName,String[] cellFormatType) 函数参数讲解
1、List list:数据库里面的数据存放在List集合中(其中第一个list数据为表头加入);
2、int columnSize:表的列数;
3、HttpServletResponse response:response请求;
4、String sheetName:表名;
5、String fileName:excel文件名,需要加上后缀名;
6、String[] cellFormatType:String[] cellFormatType = {"t","t","t"};//这个是有多少列明 就有多少个t//记住就行。
前端页面代码
<form action="UserInfoServlet?type=3" method="post">
<input type="submit" class="ydc-assetStyle-btn" value="导出">
</form>
在此处导出excel表格
![](https://img.haomeiwen.com/i2367321/8a340dc1cebfaf5f.png)
网友评论