Java如何导出Excel表格

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编辑器使用浏览器离线存储将内容保存在本地。

用户写博客的过程中,内容实时保存在浏览器缓存中,在用户关闭浏览器或者其它异常情况下,内容不会丢失。用户再次打开浏览器时,会显示上次用户正在编辑的没有发表的内容。

博客发表后,本地缓存将被删除。

用户可以选择 把正在写的博客保存到服务器草稿箱,即使换浏览器或者清除缓存,内容也不会丢失。

**注意:**虽然浏览器存储大部分时候都比较可靠,但为了您的数据安全,在联网后,请务必及时发表或者保存到服务器草稿箱

##浏览器兼容

  1. 目前,本编辑器对Chrome浏览器支持最为完整。建议大家使用较新版本的Chrome。
  2. IE9以下不支持
  3. IE9,10,11存在以下问题
    1. 不支持离线功能
    2. IE9不支持文件导入导出
    3. IE10不支持拖拽文件导入


  1. 这里是 脚注内容. ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值