直接上代码,这个是一个在webFrom中的例子,要请求的页面是ashx

 public void ExportVisaFeeAll(HttpContext context)
        {            try
            {                string title ="签证费用表";                string[] idArray = context.Request["idStr"].ToString().Split(',');
                DateTime startDate = DateTime.Parse(context.Request["startDate"]);
                DateTime endDate = DateTime.Parse(context.Request["endDate"]).AddDays(1);                string unit = context.Request.QueryString["unit"];                string groupname = context.Request.QueryString["groupname"];                string countryname = context.Request.QueryString["countryname"];                string Member = context.Request.QueryString["Member"];                string LetterNumbers = context.Request.QueryString["LetterNumbers"];
                ListvisaFees = new OilDigital.CGGL.Web.VisaApply.VisaApplyProcess().GetVisaFeeByQueryString(startDate, endDate, unit, groupname, countryname, Member, LetterNumbers);                for (int i = visaFees.Count-1; i >=0; i--)
                {                    bool flag = true;                    for (int j  = 0; j < idArray.Length; j++)
                    {                        if (visaFees[i].Id.ToString() == idArray[j])
                        {
                            flag = false;                            break;
                        }
                    }                    if (flag)
                    {
                        visaFees.RemoveAt(i);
                    }
                }
                HSSFWorkbook wb = new HSSFWorkbook();//创建工作薄
                HSSFSheet sheet = (HSSFSheet)wb.CreateSheet(title); //创建工作表
                HSSFRow row_Title = (HSSFRow)sheet.CreateRow(0); //创建列头行
                row_Title.HeightInPoints = 19.5F; //设置列头行高
                string[] titleStr = { "批件文号", "申办国家(地区)", "总金额", "人员名单", "总人数", "备注", "创建时间", "创建人"};                //设置列宽
                for (int i = 0; i < titleStr.Length; i++)
                {
                       sheet.SetColumnWidth(i, 20 * 256);

                }                //设置列样式
                HSSFCellStyle cs_Title = (HSSFCellStyle)wb.CreateCellStyle(); //创建列头样式
                cs_Title.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; //水平居中
                cs_Title.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; //垂直居中
                HSSFFont cs_Title_Font = (HSSFFont)wb.CreateFont(); //创建字体
                cs_Title_Font.FontHeightInPoints = 12; //字体大小
                cs_Title_Font.Boldweight = (short)FontBoldWeight.Bold;
                cs_Title.SetFont(cs_Title_Font); //将字体绑定到样式                //填充列头内容
                for (int i = 0; i < titleStr.Length; i++)
                {
                    HSSFCell cell_Title = (HSSFCell)row_Title.CreateCell(i); //创建单元格
                    cell_Title.CellStyle = cs_Title; //将样式绑定到单元格                    cell_Title.SetCellValue(titleStr[i]);
                }                //设置单元格样式
                HSSFCellStyle cs_Content = (HSSFCellStyle)wb.CreateCellStyle(); //创建列头样式
                cs_Content.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; //水平居中
                cs_Content.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; //垂直居中                //填充单元格
                int r = 0;                for (r = 0; r < visaFees.Count; r++)
                {                    int i = r;
                    HSSFRow row = (HSSFRow)sheet.CreateRow(i + 1);
                    row.HeightInPoints = 16;                    string[] arr = { visaFees[i].LetterNumbers.ToString(), visaFees[i].EmbassyNames, visaFees[i].Fee, visaFees[i].Names, visaFees[i].TotalAmount.ToString(), visaFees[i].Memo, visaFees[i].CreatedTime.ToString("yyyy-MM-dd"), visaFees[i].CreatedBy };                    for (int j = 0; j < arr.Length; j++)
                    {
                        HSSFCell cell = (HSSFCell)row.CreateCell(j); //创建单元格
                        cell.CellStyle = cs_Content;
                            cell.SetCellValue(arr[j]);

                    }
                }
              

                HttpResponse httpResponse = HttpContext.Current.Response;
                httpResponse.Clear();
                httpResponse.Buffer = true;                //httpResponse.Charset = Encoding.UTF8.BodyName;
                httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("签证费用.xls"));
                httpResponse.ContentEncoding = System.Text.Encoding.UTF8;
                httpResponse.ContentType = "application/vnd.ms-excel; charset=UTF-8";
                wb.Write(httpResponse.OutputStream);
                httpResponse.End();
            }            catch (Exception ex)
            {                throw new Exception(ex.Message);
            }
        }