在aspnet中将girdview中间连续几项的信息导出excel

图片效果

gridview中的样子

导出的excel中的样子

没有导出记录时


方法:

导出excel按钮下面的代码

if (gvOlders.Rows.Count == 0)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('没有记录可以导出!')</script>");
        }
        else
        {
            OutDataToExcel(gvOlders, mon);
        }

OutDataToExcel方法

 public void OutDataToExcel(System.Web.UI.WebControls.GridView gridview, IList<BasicInfo> list)
    {
        gvOlders.Columns[0].Visible = false;
        gvOlders.Columns[8].Visible = false;
        HttpContext hc = HttpContext.Current;
        ArrayList al = new ArrayList();
        int count = gridview.Columns.Count - 1;
        for (int i = 0; i < count; i++)
        {
            string t = gvOlders.Columns[i].HeaderText;
            al.Add(t);
        }
        AppLibrary.WriteExcel.XlsDocument doc = new AppLibrary.WriteExcel.XlsDocument();
        //给文件取名字
        doc.FileName = DateTime.Now.ToString("yyyyMMddHHMMss") + ".xls";
        string SheetName = string.Empty;
        SheetName = "数据查询";
        AppLibrary.WriteExcel.Worksheet sheet = doc.Workbook.Worksheets.Add(SheetName);
        AppLibrary.WriteExcel.Cells cells = sheet.Cells;
        #region 第一个样式
        AppLibrary.WriteExcel.XF XFstyle = doc.NewXF();//添加样式的   
        XFstyle.HorizontalAlignment = AppLibrary.WriteExcel.HorizontalAlignments.Centered;
        XFstyle.Font.FontName = "宋体";//字体   
        XFstyle.UseMisc = true;
        XFstyle.TextDirection = AppLibrary.WriteExcel.TextDirections.LeftToRight;//文本位置  




        XFstyle.Font.Bold = false;//加粗  
        #region 边框线的样式
        XFstyle.BottomLineStyle = 1;
        XFstyle.LeftLineStyle = 1;
        XFstyle.TopLineStyle = 1;
        XFstyle.RightLineStyle = 1;
        #endregion
        XFstyle.UseBorder = true;
        XFstyle.PatternBackgroundColor = AppLibrary.WriteExcel.Colors.Blue;
        XFstyle.PatternColor = AppLibrary.WriteExcel.Colors.White;
        XFstyle.Pattern = 1;
        #region 宽度
        AppLibrary.WriteExcel.ColumnInfo colInfo = new AppLibrary.WriteExcel.ColumnInfo(doc,


sheet);
        colInfo.ColumnIndexStart = 0;
        colInfo.ColumnIndexEnd = 4;
        colInfo.Width = 10 * 256;
        sheet.AddColumnInfo(colInfo);
        AppLibrary.WriteExcel.ColumnInfo colInfo1 = new AppLibrary.WriteExcel.ColumnInfo


(doc, sheet);
        colInfo1.ColumnIndexStart = 5;
        colInfo1.ColumnIndexEnd = 7;
        colInfo1.Width = 15 * 256;
        sheet.AddColumnInfo(colInfo1);


        AppLibrary.WriteExcel.ColumnInfo colInfo2 = new AppLibrary.WriteExcel.ColumnInfo


(doc, sheet);
        colInfo2.ColumnIndexStart = 8;
        colInfo2.ColumnIndexEnd = 9;
        colInfo2.Width = 15 * 256;
        sheet.AddColumnInfo(colInfo2);


        AppLibrary.WriteExcel.ColumnInfo colInfo3 = new AppLibrary.WriteExcel.ColumnInfo


(doc, sheet);
        colInfo3.ColumnIndexStart = 12;
        colInfo3.ColumnIndexEnd = 12;
        colInfo3.Width = 15 * 256;
        sheet.AddColumnInfo(colInfo3);


        AppLibrary.WriteExcel.ColumnInfo colInfo4 = new AppLibrary.WriteExcel.ColumnInfo


(doc, sheet);
        colInfo4.ColumnIndexStart = 13;
        colInfo4.ColumnIndexEnd = 13;
        colInfo4.Width = 50 * 256;
        sheet.AddColumnInfo(colInfo4);


        AppLibrary.WriteExcel.ColumnInfo colInfo5 = new AppLibrary.WriteExcel.ColumnInfo


(doc, sheet);
        colInfo5.ColumnIndexStart = 14;
        colInfo5.ColumnIndexEnd = 14;
        colInfo5.Width = 15 * 256;
        sheet.AddColumnInfo(colInfo5);


        AppLibrary.WriteExcel.ColumnInfo colInfo6 = new AppLibrary.WriteExcel.ColumnInfo


(doc, sheet);
        colInfo6.ColumnIndexStart = 15;
        colInfo6.ColumnIndexEnd = 15;
        colInfo6.Width = 50 * 256;
        sheet.AddColumnInfo(colInfo6);
        #endregion
        #endregion
        #region 添加表头
        int idx = 1;
        for (int i = 1; i < al.Count; i++)
        {
            cells.Add(1, idx, al[i], XFstyle);
            idx++;
        }
        #endregion
        int f = 2;//从第二行开始填充数据   
        foreach (BasicInfo m in list)
        {
            cells.Add(f, 1, m.OlderName, XFstyle);
            cells.Add(f, 2, m.Birthday.ToShortDateString(), XFstyle);
            cells.Add(f, 3, m.Age, XFstyle);
            cells.Add(f, 4, m.Sex, XFstyle);
            cells.Add(f, 5, m.Pid, XFstyle);
            cells.Add(f, 6, m.SelfDepend, XFstyle);
            cells.Add(f, 7, m.State, XFstyle);
            f++;
        }
        doc.Send();
        hc.Response.Flush();
        hc.Response.End();
    }


mon页面级变量

 public List<BasicInfo> mon
    {
        get { return ViewState["mon"] as List<BasicInfo>; }
        set { ViewState["mon"] = value; }
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值