分享

.NET导出报表汇总(一)

 悟静 2012-05-13

.NET导出报表汇总(一):

    DATASET导出报表,优点:使用方便,代码简单,缺点:不用为报表设置样式
1、DATASET导出报表
 protected void Button1_Click(object sender, EventArgs e)
        {
            // 文件名字定义
            string fileName = string.Empty;

            // 取得导入的Excel数据定义
            System.Data.DataTable dtExcelData = null;

            // 文件名字的生成
            string topic = drpTopic.SelectedIndex > 0 ? drpTopic.SelectedItem.Text : "";
            fileName = topic + DateTimePicker1.Text + "到" + DateTimePicker2.Text + "文章访问统计数据_" + System.DateTime.Now.ToString().Replace("/", "_").Substring(0, 10);
            string filePath = Server.MapPath("..\\ExcelData\\") + fileName;

            try
            {
                dtExcelData = GetData();

                // 数据有无判断
                if (dtExcelData == null || dtExcelData.Rows.Count == 0)
                {
                    return;
                }
                string directoryPath = filePath;
                if (Directory.Exists(directoryPath))
                {//do nothing
                }
                else
                {
                    Directory.CreateDirectory(directoryPath);
                }

                HttpResponse resp;
                resp = System.Web.HttpContext.Current.Response;
                resp.Clear();
                resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName) + ".xls");
                string colHeaders = "", ls_item = "";
                int i = 0;
                DataRow[] myRow = null;

                if (0 < dtExcelData.Rows.Count)
                {
                    myRow = dtExcelData.Select("");
                }


                for (i = 0; i < dtExcelData.Columns.Count; i++)
                {
                    colHeaders += dtExcelData.Columns[i].Caption.ToString() + "\t";
                }

                colHeaders += "\n";
                resp.Write(colHeaders);

                // Excel的数据部分生成
                foreach (DataRow row in myRow)
                {
                    ls_item = "";
                    for (i = 0; i < row.Table.Columns.Count; i++)
                    {
                        ls_item += row[i].ToString().Replace("\r", "").Replace("\n", "") + "\t";
                    }
                    ls_item += "\n";
                    resp.Write(ls_item);
                }

                File.Delete(fileName + ".xls");

                resp.End();
//应该修改为:HttpContext.Current.ApplicationInstance.CompleteRequest();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


2、DATASET导出报表
    protected void btnExport_Click(object sender, EventArgs e)
    {
        string excel_Year = ddl1.SelectedItem.Value;
        string excel_Month = ddl2.SelectedItem.Value;
        string strExcel_CanalId = ddlCanal.SelectedItem.Value;
        int excel_CanalId = -1;
        if (string.IsNullOrEmpty(excel_Year))
        {
            Jss.WindowAlert(this.Page, "导出数据失败!", false);
            return;
        }
        else if (string.IsNullOrEmpty(excel_Month))
        {
            Jss.WindowAlert(this.Page, "导出数据失败!", false);
            return;
        }
        else if (!int.TryParse(strExcel_CanalId, out excel_CanalId))
        {
            Jss.WindowAlert(this.Page, "导出数据失败!", false);
            return;
        }
        DataSet ds = new DataSet();
        System.Data.DataTable dtExcelData = new DataTable();
        DataTable tsolved = new DataTable();   //最终数据的载体。

        ds = dalRT.GetDataSet(excel_Year, excel_Month, excel_CanalId);
        if (ds == null)
        {
            Jss.WindowAlert(this, "当前没有可以导出的数据!", false);
            return;
        }
        tsolved = ds.Tables[0];

        dtExcelData.Columns.Add("序号");
        dtExcelData.Columns.Add("组别");
        dtExcelData.Columns.Add("网络渠道");
        dtExcelData.Columns.Add("套餐版本");
        dtExcelData.Columns.Add("端口单价");
        dtExcelData.Columns.Add("使用人");
        dtExcelData.Columns.Add("分行电话");
        dtExcelData.Columns.Add("开通人手机号");
        dtExcelData.Columns.Add("端口用户名");
        dtExcelData.Columns.Add("开通方式");
        dtExcelData.Columns.Add("备注");

        if (tsolved.Rows.Count > 0)
        {
            for (int i = 0; i < tsolved.Rows.Count; i++)
            {
                DataRow dr = dtExcelData.NewRow();
                dr[0] = tsolved.Rows[i]["Id"];
                dr[1] = tsolved.Rows[i]["Groups"];
                dr[2] = tsolved.Rows[i]["Canal_Name"];
                dr[3] = tsolved.Rows[i]["Product_Name"];
                dr[4] = tsolved.Rows[i]["Port_Price"];
                dr[5] = tsolved.Rows[i]["Staff_Name"];
                dr[6] = tsolved.Rows[i]["Groups_TelPhone_Number"];
                dr[7] = tsolved.Rows[i]["Mobile_Number"];
                dr[8] = tsolved.Rows[i]["Port_UserName"];
                dr[9] = tsolved.Rows[i]["Status"];
                dr[10] = tsolved.Rows[i]["Notes"];
                dtExcelData.Rows.Add(dr);
            }
        }
        else
        {
            Jss.WindowAlert(this, "当前没有可以导出的数据!", false);
            return;
        }

        // 文件名字定义
        string fileName = string.Empty;
        // 文件名字的生成

        fileName = System.DateTime.Now.ToString().Replace("/", "_").Substring(0, 7)+"_网站表";
        string filePath = Server.MapPath("ExcelData\\") + fileName;

        try
        {

            // 数据有无判断
            if (dtExcelData == null || dtExcelData.Rows.Count == 0)
            {
                return;
            }
            string directoryPath = filePath;
            if (Directory.Exists(directoryPath))
            {//do nothing
            }
            else
            {
                Directory.CreateDirectory(directoryPath);
            }


            HttpResponse resp;
            resp = System.Web.HttpContext.Current.Response;
            resp.Clear();
            resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(fileName) + ".xls");
            string colHeaders = "", ls_item = "";
            int i = 0;
            DataRow[] myRow = null;

            //
            if (0 < dtExcelData.Rows.Count)
            {
                myRow = dtExcelData.Select("");
            }


            for (i = 0; i < dtExcelData.Columns.Count; i++)
            {
                colHeaders += dtExcelData.Columns[i].Caption.ToString() + "\t";
            }

            colHeaders += "\n";
            resp.Write(colHeaders);

            // Excel的数据部分生成
            foreach (DataRow row in myRow)
            {
                ls_item = "";
                for (i = 0; i < row.Table.Columns.Count; i++)
                {
                    ls_item += row[i].ToString().Replace("\r", "").Replace("\n", "") + "\t";
                }
                ls_item += "\n";
                resp.Write(ls_item);
            }

            File.Delete(fileName + ".xls");

            resp.End();
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约