mvc导出excel

   导出excel,怎么导出呢?这个原理是什么呢?

   首先在html页面添加单击方法,触发controllers中的方法。怎么触发controllers中导出的方法呢?

   在js中的事件中可以这样写。

function xport() {
    
    window.location.href = "/UserManager/OutoExcel";
}
大家也知道OutoExcel就是contrlers中导出的方法了。

      其实导出的数据,不是像机房收费系统中从ui表格中得到数据。这个是怎么做呢?举一个例子,查询所有的用户,查到的结果用一个list来接收,然后在返回到js中显示到html页面中。现在把list变成staic类型的。成了静态全局变量。

    这样在调用这个list给excel的各个表格赋值,这样excel就做好了。重要的事情就是list要变成静态全局变量。

    来看一下controllers的代码。

public void OutoExcel() {

            try
            {

                string title = "人员信息统计";
                NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");

                NPOI.SS.UserModel.IRow headerrow = sheet.CreateRow(0);
                ICellStyle style = book.CreateCellStyle();
               // HorizontalAlignment.Center;
                style.Alignment = HorizontalAlignment.Center;
                style.VerticalAlignment = VerticalAlignment.Center;

                #region 表格标题设置 表格的表头
                ICell cell = headerrow.CreateCell(0);
                cell.CellStyle = style;
                cell.SetCellValue("用户账号");
                cell = headerrow.CreateCell(1);
                cell.CellStyle = style;
                cell.SetCellValue("级别");
                cell = headerrow.CreateCell(2);
                cell.CellStyle = style;
                cell.SetCellValue("姓名");
                cell = headerrow.CreateCell(3);
                cell.CellStyle = style;
                cell.SetCellValue("电话");
                cell = headerrow.CreateCell(4);
                cell.CellStyle = style;
                cell.SetCellValue("邮箱");
                cell = headerrow.CreateCell(5);
                cell.CellStyle = style;
                cell.SetCellValue("备注");
                //cell = headerrow.CreateCell(6);
                //cell.CellStyle = style;
                //cell.SetCellValue("操作列");
                #endregion

               
                #region 根据标题for循环填充excel表格
                for (int i = 0; i < list.Count; i++)
                {
                    IRow row = sheet.CreateRow(i + 1);
                    cell = row.CreateCell(0);
                    cell.SetCellValue(list[i].Userid.ToString());
                    cell = row.CreateCell(1);
                    cell.SetCellValue(list[i].Commentid.ToString());  //关键在于list list设置成了静态变量(可以理解成全局变量)
                    cell = row.CreateCell(2);
                    cell.SetCellValue(list[i].Username.ToString());
                    cell = row.CreateCell(3);
                    cell.SetCellValue(list[i].Tel.ToString());
                    cell = row.CreateCell(4);
                    cell.SetCellValue(list[i].Mail.ToString());
                    cell = row.CreateCell(5);
                    cell.SetCellValue(list[i].Remark.ToString());
                    //cell = row.CreateCell(6);
                    //cell.SetCellValue(list[i].);
                }
                #region 导出
                //弹出导出的界面
                MemoryStream ms = new MemoryStream();
                book.Write(ms);
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", title + "_" + DateTime.Now.ToString("yyyy-MM-dd"), System.Text.Encoding.UTF8));
                Response.BinaryWrite(ms.ToArray());
                Response.End();
                book = null;
                ms.Close();
                ms.Dispose();
                #endregion

                #endregion
              


            }
            catch (Exception)
            {

                throw new Exception("导出遇到错误,请联系管理员");
            }

        }
其中,NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");

                NPOI.SS.UserModel.IRow headerrow = sheet.CreateRow(0); 这段代码的使用需要使用引入命名空间。

using System.IO;
using NPOI;
using NPOI.HPSF;
using NPOI.HSSF;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;

其中还需要引用NPOI 2.2.1 binary package的几个dll。才能在命名空间上写出。下载地址 http://npoi.codeplex.com/downloads/get/1476595

  







JSP(JavaServer Pages)是一种动态网页技术,允许开发者将Java代码嵌入到HTML页面中。当Web服务器接收到对JSP页面的请求时,它将执行页面中的Java代码,并将结果嵌入到生成的HTML中发送给客户端。 MVC(Model-View-Controller)是一种设计模式,用于将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。模型负责数据和业务逻辑,视图负责展示数据,控制器负责接收用户输入并调用模型和视图去完成用户的请求。 导出Excel通常是指将Web应用中的数据导出为Microsoft Excel可识别的文件格式,比如.xlsx或.xls。在JSP中使用MVC模式导出Excel,通常需要以下步骤: 1. 创建数据模型(Model):定义需要导出的数据结构。 2. 创建视图(View):设计导出数据的布局,通常是HTML表格。 3. 创建控制器(Controller):编写处理导出逻辑的代码,包括接收用户的导出请求、数据的检索和转换、以及响应的生成和发送。 具体到导出Excel,控制器需要生成Excel文件的数据流。可以使用Apache POI库来帮助处理Excel文件的创建和写入。然后,控制器会将这个文件流作为HTTP响应发送给客户端,通常会设置响应头,告知浏览器这是一个附件,浏览器会提示用户保存文件。 纯手写代码的话,你需要处理以下几个方面: - Java代码的组织,确保按照MVC模式分离逻辑。 - 在视图层面,通过JSP标签生成静态HTML表格。 - 在控制器层面,使用Java代码处理HTTP请求和响应,以及调用Apache POI或其他库来生成Excel文件。 - 设置正确的HTTP响应头,使得浏览器能够识别并处理生成的文件。 以下是一个简化的示例步骤: 1. 引入Apache POI依赖到你的项目中。 2. 在控制器中,编写导出数据的逻辑,创建一个HSSFWorkbook或XSSFWorkbook对象(取决于你想要创建的是旧版的.xls还是新版的.xlsx文件)。 3. 使用Apache POI提供的API填充工作簿、工作表、行和单元格等。 4. 将工作簿对象写入到HTTP响应输出流中。 5. 设置响应的内容类型为Excel文件类型,并添加头信息提示浏览器将响应作为文件下载。 这里没有提供具体的代码实现,因为编写完整的代码超出了简短回答的范围,但它可以根据上述步骤来完成。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值