数据库中数据,导出excel

这是提交方式..

document.location.href="servlet/ExportServlet";

servlet中
{
        String fileName = "exportBOM.xls";
        try {  
            fileName = new String(fileName.getBytes(), "iso8859-1");//解决中文 文件名问题  
        } catch (UnsupportedEncodingException e1) {  
            e1.printStackTrace();  
        }  
        response.setHeader("content-disposition", "attachment; filename="+ fileName);// 设定输出文件头  
        response.setContentType("application/msexcel;charset=UTF-8");// 定义输出类型  
        getInputStream(response,bomId);
}
public void getInputStream(HttpServletResponse response,String bomId) {  
		 BCompGoods bg=new BCompGoods();
        try {  
            OutputStream os = response.getOutputStream();  
            WritableWorkbook wwb = Workbook.createWorkbook(os);  
            WritableSheet ws = wwb.createSheet("Sheet1", 0);  
            WritableFont wfc = new WritableFont(WritableFont.ARIAL, 8,  
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,  
                    Colour.BLACK);  
            WritableCellFormat wcfFC = new WritableCellFormat(wfc); 
            Label label1 = new Label(0, 0, "物料编号", wcfFC);  
            ws.addCell(label1);  
            Label label2 = new Label(1, 0, "物料名称", wcfFC);  
            ws.addCell(label2);  
            Label label3 = new Label(2, 4, "规格型号", wcfFC);  
            ws.addCell(label3);  
            Label label4 = new Label(3, 0, "封装", wcfFC);  
            ws.addCell(label4);  
            Label label5 = new Label(4, 0, "单位", wcfFC);  
            ws.addCell(label5);  
            Label label6 = new Label(5, 0, "子BOM编号", wcfFC);  
            ws.addCell(label6);  
            Label label7 = new Label(6, 0, "子BOM名称", wcfFC);  
            ws.addCell(label7);  
            Label label8 = new Label(7, 0, "子项数量", wcfFC);  
            ws.addCell(label8);  
            Label label9 = new Label(8, 0, "仓库名称", wcfFC);  
            ws.addCell(label9); 
    			Resultobj rt=bg.searchBOM(bomId);
    			for(int i=1;i<rt.getRows();i++){
    				Label name = new Label(0, i, rt.getCell("CODE", i));  
                    ws.addCell(name);  
                    Label num = new Label(1, i, rt.getCell("SHORTNAME", i));  
                    ws.addCell(num);  
                    Label num2 = new Label(2, i, rt.getCell("STYLE", i));  
                    ws.addCell(num2); 
                    Label num3 = new Label(3, i, rt.getCell("APPROVALNO", i));  
                    ws.addCell(num3); 
                    Label num4 = new Label(4, i, rt.getCell("Name", i));  
                    ws.addCell(num4); 
                    Label num5 = new Label(5, i, rt.getCell("SUBBOMCODE", i));  
                    ws.addCell(num5); 
                    Label num6 = new Label(6, i, rt.getCell("SUBBOMNAME", i));  
                    ws.addCell(num6); 
                    Label num7 = new Label(7, i, rt.getCell("STANDBATCH", i));  
                    ws.addCell(num7); 
                    Label num8 = new Label(8, i, rt.getCell("storeName", i));  
                    ws.addCell(num8); 
    			}
//关闭操作  
            wwb.write();  
            wwb.close();  
            os.close();
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (RowsExceededException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (WriteException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (GeneralException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    }  
 就完成  导出excel了.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
A:要在.NET MVC应用程序使用EPPlus库将数据库数据导出Excel,可以按照以下步骤进行操作: 1. 首先,在项目安装EPPlus包。 2. 然后,创建一个控制器处理导出数据。在控制器,可以使用Entity Framework或其他ORM库从数据库检索所需数据。 3. 接下来,使用EPPlus创建一个新的Excel文档,并创建一个工作表。 4. 再然后,将从数据库检索到的数据填充到工作表。使用适当的数据类型格式化单元格。 5. 最后,将Excel文档保存到服务器上的某个位置,然后将其提供给用户进行下载。 下面是一个示例控制器代码: ``` public ActionResult ExportToExcel() { var dataList = dbContext.MyTable.ToList(); var excelPackage = new ExcelPackage(); var worksheet = excelPackage.Workbook.Worksheets.Add("My Data"); worksheet.Cells[1, 1].Value = "Column 1"; worksheet.Cells[1, 2].Value = "Column 2"; worksheet.Cells[1, 3].Value = "Column 3"; int rowIndex = 2; foreach (var dataItem in dataList) { worksheet.Cells[rowIndex, 1].Value = dataItem.Column1; worksheet.Cells[rowIndex, 2].Value = dataItem.Column2; worksheet.Cells[rowIndex, 3].Value = dataItem.Column3; rowIndex++; } var memoryStream = new MemoryStream(); excelPackage.SaveAs(memoryStream); memoryStream.Position = 0; return File(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "MyData.xlsx"); } ``` 这个示例将从名为"MyTable"的表检索数据,并将它们填充到一个名为"My Data"的Excel工作表。最后,它将Excel文档返回给用户进行下载。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值