asp.net中实现,用户根据自己的需要,勾选checkbox,然后把查询到数据库中的内容导出到excel

来源:互联网 发布:软件概要设计文档 编辑:程序博客网 时间:2024/06/06 00:20

此类需要使用Excel对象,需先添加该对象的引用:

右键项目,添加引用,选择.NET,然后在里面选择Microsoft.Office.Interop.Excel  12.0.0.0,单击确定

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//using Word = Microsoft.Office.Interop.Word;
using System.Threading;
using office = Microsoft.Office.Core;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;

public partial class qiantai_XiZhuRenManage_XZRdaochu : System.Web.UI.Page
{
    Commonclass cc = new Commonclass();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string neirong = "";

        string BiaoTou = "";

        if (CheckBox1.Checked || CheckBox3.Checked || CheckBox4.Checked || CheckBox5.Checked || CheckBox6.Checked)
        {

            if (CheckBox1.Checked)
            { neirong += ",bumen"; BiaoTou += "," + CheckBox1.Text; }//动态添加select语句的内容和导出到excel中时的表头内容
            if (CheckBox3.Checked)
            { neirong += ",zply"; BiaoTou += "," + CheckBox3.Text; }
            if (CheckBox4.Checked)
            { neirong += ",kemu"; BiaoTou += "," + CheckBox4.Text; }
            if (CheckBox5.Checked)
            { neirong += ",proname"; BiaoTou += "," + CheckBox5.Text; }
            if (CheckBox6.Checked)
            { neirong += ",dwmc"; BiaoTou += "," + CheckBox6.Text; }

            string NeiRong = neirong.Substring(1);
            BiaoTou = BiaoTou.Substring(1);
            string[] biaotou = BiaoTou.Split(',');
            string strSql = "select " + neirong.Substring(1) + " from Tb_program where state='" + DropDownList2.SelectedValue.ToString() + "'and tuanweistate='" + DropDownList4.SelectedValue.ToString() + "' and zply='" + DropDownList3.SelectedValue.ToString() + "' and kemu='" + Dpkemu.SelectedValue.ToString() + "'";
            DataTable dstable = cc.GetDataSetStr(strSql, "tbB1");
            if (dstable.Rows.Count == 0)
            { Response.Write(cc.MessageBox("没有该状态项目!")); }
            else
            {
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
                excel.Application.Workbooks.Add(true);//在Excel中添加一个工作簿
                excel.Visible = true;//设置Excel显示
                for (int i = 0; i < biaotou.Length; i++)
                {
                    excel.Cells[1, i + 1] = biaotou[i].ToString();

                }
                for (int i = 0; i < dstable.Rows.Count; i++)
                {
                    for (int j = 0; j < dstable.Columns.Count; j++)
                    {
                        excel.Cells[i + 2, j + 1] = dstable.Rows[i][j].ToString();
                    }
                }
            }
        }
        else
        {
            Response.Write(cc.MessageBox("请勾选需要导出的内容!"));
        }


 
       

    }
}

 

 

 

 

0 0