Java如何导出Excel表格
- Markdown和扩展Markdown简洁的语法
- 代码块高亮
代码块
代码块语法遵循标准markdown代码,例如:
//<!—Servlet 界面直接调用就可以了,这里的小数类型···-->
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;
//导出 Excel
public void getGenerateExcel (HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException {
// TODO Auto-generated method stub
CommodityService commoNameService = new CommodityServiceImpl();
String currentPageStr = req.getParameter("curPage");//当前页
String pageSizeStr = req.getParameter("pageSize");//每一页的数据条数
String names = req.getParameter("cxuserName");//获取界面的用户名称
//转乱码
String commoName = Tools.ISOtoUTF8(names);
int totalAll = commoNameService.getTotalRow(commoName);//获取总行数
int currentPage = 1;
int pageSize = 10;
if(currentPageStr != null && Tools.isNum(currentPageStr)){
currentPage = Integer.parseInt(currentPageStr);
}
if(pageSizeStr != null && Tools.isNum(pageSizeStr)){
pageSize = Integer.parseInt(pageSizeStr);
}
int startIndex = (currentPage-1) * pageSize;
//List<commoditytb> TabCommodity = commoNameService.findAll(startIndex,pageSize,commoName);
List<commoditytb> list = new ArrayList<commoditytb>();
list = commoNameService.findAll(startIndex,totalAll,commoName);
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("商品表一");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
/*style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式*/
HSSFCell cell = row.createCell(0);
cell.setCellValue("商品名称");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("商品类型");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("销售价格");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("积分");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("库存数量");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("生产许可证");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("厂名");
cell.setCellStyle(style);
cell = row.createCell(7);
cell.setCellValue("厂址");
cell.setCellStyle(style);
cell = row.createCell(8);
cell.setCellValue("厂家联系电话");
cell.setCellStyle(style);
cell = row.createCell(9);
cell.setCellValue("配料表");
cell.setCellStyle(style);
cell = row.createCell(10);
cell.setCellValue("保质期");
cell.setCellStyle(style);
cell = row.createCell(11);
cell.setCellValue("净含量");
cell.setCellStyle(style);
cell = row.createCell(12);
cell.setCellValue("包装方式");
cell.setCellStyle(style);
cell = row.createCell(13);
cell.setCellValue("品牌");
cell.setCellStyle(style);
cell = row.createCell(14);
cell.setCellValue("存储方式");
cell.setCellStyle(style);
cell = row.createCell(15);
cell.setCellValue("下架状态");
cell.setCellStyle(style);
cell = row.createCell(16);
cell.setCellValue("商品编号");
cell.setCellStyle(style);
for (int i = 0; i < list.size(); i++)
{
row = sheet.createRow((int) i + 1);
commoditytb commodity = (commoditytb)list.get(i);
// 第五步,创建单元格,并设置值
row.createCell(0).setCellValue(commodity.getCommodityName());//商品名称
row.createCell(1).setCellValue(commodity.getCommodityTypeName());//商品类型
row.createCell(2).setCellValue((commodity.getSellPrice()).toString());//销售价格 setSellPrice
row.createCell(3).setCellValue((commodity.getGetPoints()));//所获积分
row.createCell(4).setCellValue(commodity.getStockNumber());//库存数量
row.createCell(5).setCellValue(commodity.getProduceNumber());//生产许可证
row.createCell(6).setCellValue(commodity.getFactoryName());//厂名
row.createCell(7).setCellValue(commodity.getFactoryAdress());//厂址
row.createCell(8).setCellValue(commodity.getFactoryConnection());//厂家联系电话
row.createCell(9).setCellValue(commodity.getChargeMixture());//配料表
row.createCell(10).setCellValue(commodity.getQualityDate());//保质期
row.createCell(11).setCellValue(commodity.getNetContent());//净含量
row.createCell(12).setCellValue(commodity.getPackWay());//包装方式
row.createCell(13).setCellValue(commodity.getBrank());//品牌
row.createCell(14).setCellValue(commodity.getLayInWay());//存储方式
row.createCell(15).setCellValue(commodity.getSoldOutBit());//下架状态
row.createCell(16).setCellValue(commodity.getProductID());//商品编号
}
// 第六步,将文件存到指定位置
try
{
Date date = new Date();
String str = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSSS").format(date); //需要文件导出带有时间的,请把时间拼接到/杠后面去
FileOutputStream fout = new FileOutputStream("C:\\Users\\夜\\Desktop\\商品图片/" + str + "Table.xls");
wb.write(fout);
fout.close();
}catch (Exception e)
{
e.printStackTrace();
}
}
###脚注
生成一个脚注1.
目录
离线写博客
即使用户在没有网络的情况下,也可以通过本编辑器离线写博客(直接在曾经使用过的浏览器中输入write.blog.csdn.net/mdeditor即可。Markdown编辑器使用浏览器离线存储将内容保存在本地。
用户写博客的过程中,内容实时保存在浏览器缓存中,在用户关闭浏览器或者其它异常情况下,内容不会丢失。用户再次打开浏览器时,会显示上次用户正在编辑的没有发表的内容。
博客发表后,本地缓存将被删除。
用户可以选择 把正在写的博客保存到服务器草稿箱,即使换浏览器或者清除缓存,内容也不会丢失。
**注意:**虽然浏览器存储大部分时候都比较可靠,但为了您的数据安全,在联网后,请务必及时发表或者保存到服务器草稿箱。
##浏览器兼容
- 目前,本编辑器对Chrome浏览器支持最为完整。建议大家使用较新版本的Chrome。
- IE9以下不支持
- IE9,10,11存在以下问题
- 不支持离线功能
- IE9不支持文件导入导出
- IE10不支持拖拽文件导入
这里是 脚注 的 内容. ↩︎