zoukankan      html  css  js  c++  java
  • 泛型数据生成Excel

    工作需要.提供下我的代码.直接上代码了,挺简单的.不需要怎么解释了

    protected void btnExport_Click(object sender, EventArgs e)
            {

                List
    <Bonus.Model.LogMemberBonusOut> ExportExcel = new Bonus.BLL.LogMemberBonusOut().GetList(110000, Session["strWhere"].ToString());
                DataSet ds 
    = ChangeParantIDToParentExcel(ExportExcel);
                
    string ExcelName = "PlayerFundOutListing_" + DateTime.Now.ToString("yyyyMMdd"+ ".xls";
                CreateExcelFromTable(ds.Tables[
    0], ExcelName);
            }

            
    #region 生成Excel的代码
            
    private void CreateExcelFromTable(DataTable table, string FileName)
            {
                HttpResponse response 
    = Page.Response;
                response.ContentEncoding 
    = System.Text.Encoding.GetEncoding("GBK");
                response.AddHeader(
    "Content-Disposition""attachment;filename=" + FileName);
                
    int index = 0;
                
    string headers = "";
                
    for (index = 0; index < table.Columns.Count; index++)
                {
                    headers 
    += table.Columns[index].ColumnName + "\t";
                }
                headers 
    += "\n";
                response.Write(headers);
                response.Flush();

                
    foreach (DataRow row in table.Rows)
                {
                    
    string rowContent = "";
                    
    foreach (DataColumn column in table.Columns)
                    {
                        rowContent 
    += row[column.ColumnName].ToString() + "\t";
                    }
                    rowContent 
    += "\n";
                    response.Write(rowContent);
                    response.Flush();
                }
                response.End();
            }

            
    private DataSet ChangeParantIDToParentExcel(List<Bonus.Model.LogMemberBonusOut> ListData)
            {
                DataSet ds 
    = new DataSet();
                DataTable tmpDt 
    = new DataTable();

                tmpDt.Columns.Add(
    "TranID");
                tmpDt.Columns.Add(
    "Member Code");
                tmpDt.Columns.Add(
    "Fund Out Amt");
                tmpDt.Columns.Add(
    "Bonus");
                tmpDt.Columns.Add(
    "Product");
                tmpDt.Columns.Add(
    "Fund Out Date");
                tmpDt.Columns.Add(
    "Status");
                tmpDt.Columns.Add(
    "Verified By");
                tmpDt.Columns.Add(
    "Verified Date");

                DataRow dr;

                
    foreach (var item in ListData)
                {
                    dr 
    = tmpDt.NewRow();

                    dr[
    "TranID"= item.OrderID;
                    dr[
    "Member Code"= item.MemberCode;
                    dr[
    "Fund Out Amt"= item.OutMoney;
                    dr[
    "Bonus"= item.Bonus;
                    dr[
    "Product"= GetPname(Convert.ToInt16(item.ProductIDInfo));
                    dr[
    "Fund Out Date"= item.OutDate.ToString("yyyy-MM-dd hh:mm:ss");
                    dr[
    "Status"= item.StatusID;
                    dr[
    "Verified By"= item.CreateBy;
                    dr[
    "Verified Date"= item.CreateDate.ToString("yyyy-MM-dd hh:mm:ss").Replace("0001-01-01 12:00:00""");

                    tmpDt.Rows.Add(dr);
                }

                tmpDt.AcceptChanges();
                ds.Tables.Add(tmpDt);

                
    return ds;
            }
  • 相关阅读:
    alpha冲刺9
    alpha冲刺8
    alpha冲刺7
    alpha冲刺6
    团队作业——随堂小测(同学录)
    alpha冲刺5
    alpha冲刺4
    alpha冲刺3
    设计模式——桥接模式
    Java基础——关键字
  • 原文地址:https://www.cnblogs.com/SOSOS/p/1605452.html
Copyright © 2011-2022 走看看