http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2566|回复: 1

[其他] 导出数据到csv文件 帮忙看看

[复制链接]
发表于 2013-6-6 21:56:06 | 显示全部楼层 |阅读模式
[code=csharp]using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using LogisticsSystem.BLL;


namespace LogisticsSystem.Web.UI.Invoice
{
    public partial class OutputInvoiceManage : System.Web.UI.Page
    {
        protected Button btnSearch;
        protected Button Button1;
        protected CalendarExtender Calendarextender1;
        protected CalendarExtender Calendarextender3;      
        protected HyperLink HyperLink1;
        protected ObjectDataSource ObjectDataSource1;

        private void BindData()
        {
            this.gvItemFlow.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            gvItemFlow.DataBind();

        }
        public string BindhFlowState(object flowStates)
        {
            string flowState = string.Empty;
            switch (flowStates.ToString())
            {
                case "-2": flowState = "待审核"; break;
                case "-1": flowState = "退回修改"; break;
                case "0": flowState = "申请开票"; break;
                case "1": flowState = "已开票"; break;
                case "2": flowState = "已收费"; break;
                case "3": flowState = "完成"; break;
            }
            return flowState;
        }
        public string BindhAuditFlowName(object flowStates)
        {

            return new BLL.AuditFlowManage().GetAuditFlowByAuditFlowID(Convert.ToInt32(flowStates.ToString())).ReportNumber;
        }

        public string BindhItemFlowName(object flowStates)
        {

            return new BLL.ItemFlowManage().GetItemFlowByID(Convert.ToInt32(flowStates.ToString())).FlowNumber;
        }

        public string BindhCompanyName(object companyID)
        {
            string companyName = string.Empty;

            //switch (flowStates.ToString())
            //{
            //    case "0": flowState = "未分配"; break;
            //    case "1": flowState = "已分配"; break;
            //    case "2": flowState = "实施中"; break;
            //    case "3": flowState = "完成"; break;
            //}
            CompanyManage cp = new CompanyManage();

            companyName = cp.GetCompanyNameByCompanyID(Convert.ToInt32(companyID));
            return companyName;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            InvoiceManage ivnm = new InvoiceManage();
            CompanyManage cpm = new CompanyManage();
            UserManage usm = new UserManage();

            AuditFlowManage afm = new AuditFlowManage();
            List<Invoice> list = ivnm.GetOutSelectInvoice(this.ddlOfficeTpye.Text, this.txtCompanyName.Text, this.ddlState.SelectedValue, this.txttime1.Text, this.txttime2.Text);
            HttpContext.Current.Response.Clear();
            StringWriter sw = new StringWriter();
            int iColCount = list.Count;
            sw.Write("发票号码,开票时间,开票抬头,开票内容,金额,部门,作业人,来源人,注师");
            sw.Write(",事务所,审计单位,发票种类,发票状态,备注");
            sw.Write(sw.NewLine);
            for (int i = 0; i < iColCount; i++)
            {
                Invoice inv = list;
                sw.Write(string.Concat(new object[] { inv.IntendExpend.Replace(",", "、"), ",", inv.BeginTime.ToLongDateString(), ",", inv.InvoiceName, ",", inv.InvoiceContent.Replace(",", "、"), ",", inv.InvoiceSumCost, ",", usm.GetUserByUserID(inv.UserID.ToString()).Department, ",", usm.GetUserByUserID(inv.UserID.ToString()).UserName, ",", cpm.GetOneCompany(inv.CompanyID.ToString()).Source.Replace(",", "、") }));
                if (inv.AuditFlowID > 0)
                {
                    sw.Write("," + afm.GetAuditFlowByAuditFlowID(inv.AuditFlowID).SignatureName.Replace(",", "、"));
                }
                else
                {
                    sw.Write(",");
                }
                sw.Write("," + inv.OfficeTpye + "," + cpm.GetCompanyNameByCompanyID(inv.CompanyID) + "," + ((inv.InvoiceType == 1) ? "专用发票" : "普通发票") + "," + this.BindhFlowState(inv.State) + "," + inv.Remark.Replace(",", "、"));
                sw.Write(sw.NewLine);
            }
            sw.Close();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + this.txttime1.Text + "~" + this.txttime2.Text + ".csv");
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.Write(sw);
            HttpContext.Current.Response.End();
        }

        public void ExExcel<T>(List<T> objList, string FileName, Dictionary<string, string> columnInfo)
        {
            if ((columnInfo.Count != 0) && (objList.Count != 0))
            {
                string excelStr = "";
                Type myType = objList[0].GetType();
                List<PropertyInfo> myPro = new List<PropertyInfo>();
                foreach (string cName in columnInfo.Keys)
                {
                    PropertyInfo p = myType.GetProperty(cName);
                    if (p != null)
                    {
                        myPro.Add(p);
                        excelStr = excelStr + columnInfo[cName] + "\t";
                    }
                }
                if (myPro.Count != 0)
                {
                    excelStr = excelStr + "\n";
                    foreach (T obj in objList)
                    {
                        foreach (PropertyInfo p in myPro)
                        {
                            excelStr = excelStr + p.GetValue(obj, null) + "\t";
                        }
                        excelStr = excelStr + "\n";
                    }
                    HttpResponse rs = HttpContext.Current.Response;
                    rs.ContentEncoding = Encoding.GetEncoding("GB2312");
                    rs.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
                    rs.ContentType = "application/ms-excel";
                    rs.Write(excelStr);
                    rs.End();
                }
            }
        }

        protected void gvItemFlow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvItemFlow.PageIndex = e.NewPageIndex;
            this.BindData();
        }

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

        public void ToExcel(Control ctl)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = Encoding.Default;
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            ctl.Page.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
        }

        private string ConnectionString
        {
            get
            {
                return "Server=localhost;Database=Northwind;\r\n        Trusted_Connection=true";
            }
        }
    }
}[/code]

错误提示:错误        “LogisticsSystem.Web.UI.Invoice”是“命名空间”,但此处被当做“类型”来使用




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-6-6 22:04:32 | 显示全部楼层
http://www.sufeinet.com/thread-4-1-1.html 你参考下我的文章吧,这里有好多方法,你可以选择别的方法
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-18 07:38

© 2014-2021

快速回复 返回顶部 返回列表