asp.net中的ExportToExcel

时间:2013-12-05 08:43:28

标签: c# asp.net

 Destinations      || DeparturePort  || Count || % of Search ||
 --------------------------------------------------------------
 Caribbean Eastern || FLL,MIA     || 2     || 0.03           ||
 South America     || LIM         || 1     || 0.02           ||

导出到excel的代码如下:

using (System.IO.StreamWriter tempFile = new System.IO.StreamWriter(Response.OutputStream))
{
    string cols = "";

    foreach (DataColumn dc in dt1.Columns)
    {
        cols += dc.ColumnName.ToString() + ",";
    }

    #region Writetofile
    System.Collections.Generic.List<string> fileRows = new System.Collections.Generic.List<string>();

    for (int i = 0; i < dt1.Rows.Count; i++)
    {
        string strToWrite = string.Empty;

        for (int j = 0; j < dt1.Columns.Count; j++)
        {
            if (dt1.Rows[i][j].ToString().IndexOf(',') != 0)
            {
                strToWrite += "\"" + dt1.Rows[i][j].ToString() + "," + "\"";
            }
            else
                strToWrite += dt1.Rows[i][j].ToString() + ",";
        }
        fileRows.Add(strToWrite);
    }
    #endregion

    this.CreateExportFile(cols, fileRows, fileName, string.Empty);
    Response.End();
}

在excel中导出的实际代码如下:

internal void CreateExportFile(string colHeader, List<string> arrL, string fileName, string fileType)
{
    #region New code
    if(arrL != null && arrL.Count > 0)
    {
        Response.ContentType = (fileType == null || fileType == string.Empty) ? "text/csv" : fileType;
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);

        using(System.IO.StreamWriter sw = new System.IO.StreamWriter(Response.OutputStream))
        {
            sw.WriteLine(colHeader);

            for(int i = 0; i < arrL.Count; i++)
                sw.WriteLine(arrL[i]);

            sw.Flush();
        } // using 
        Response.End();
    } // if we have data
    #endregion
}

当Column具有单个值时,这适用。但是由于列具有多个值作为MIA,因此FLL将它们分隔在不同的列中。我可以在这做什么改变。?

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

您可以使用以下使用反射的类,基本上将您的行建模为代码中的类,并为每个条目创建一个对象,将数据作为List传递,并且您的列将是该类中的属性。

对于多个值,您可以创建一个类DeparturePort,例如包含列表FLL,MIA和返回“FLL,MIA”的ToString,并且主记录类中的属性将包含DeparturePort的实例。 / p>

  public class CsvExport<T> where T : class
    {
        /// <summary>
        /// The objects
        /// </summary>
        public List<T> Objects;

        /// <summary>
        /// Initializes a new instance of the <see cref="CsvExport{T}"/> class.
        /// </summary>
        /// <param name="objects">The objects.</param>
        public CsvExport(List<T> objects)
        {
            Objects = objects;
        }

        /// <summary>
        /// Exports this instance.
        /// </summary>
        /// <returns></returns>
        public string Export()
        {
            return Export(true);
        }

        /// <summary>
        /// Exports the specified include header line.
        /// </summary>
        /// <param name="includeHeaderLine">if set to <c>true</c> [include header line].</param>
        /// <returns></returns>
        public string Export(bool includeHeaderLine)
        {

            StringBuilder sb = new StringBuilder();
            //Get properties using reflection.
            IList<PropertyInfo> propertyInfos = typeof(T).GetProperties();

            if (includeHeaderLine)
            {
                //add header line.
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    sb.Append(propertyInfo.Name).Append(",");
                }
                sb.Remove(sb.Length - 1, 1).AppendLine();
            }

            //add value for each property.
            foreach (T obj in Objects)
            {
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    sb.Append(MakeValueCsvFriendly(propertyInfo.GetValue(obj, null))).Append(",");
                }
                sb.Remove(sb.Length - 1, 1).AppendLine();
            }

            return sb.ToString();
        }

        //export to a file.
        /// <summary>
        /// Exports the automatic file.
        /// </summary>
        /// <param name="path">The path.</param>
        public void ExportToFile(string path)
        {
            File.WriteAllText(path, Export());
        }

        //export as binary data.
        /// <summary>
        /// Exports the automatic bytes.
        /// </summary>
        /// <returns></returns>
        public byte[] ExportToBytes()
        {
            return Encoding.UTF8.GetBytes(Export());
        }

        //get the csv value for field.
        /// <summary>
        /// Makes the value CSV friendly.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns></returns>
        private string MakeValueCsvFriendly(object value)
        {
            if (value == null) return "";
            if (value is Nullable && ((INullable)value).IsNull) return "";

            if (value is DateTime)
            {
                if (((DateTime)value).TimeOfDay.TotalSeconds == 0)
                    return ((DateTime)value).ToString("yyyy-MM-dd");
                return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
            }
            string output = value.ToString();

            if (output.Contains(",") || output.Contains("\""))
                output = '"' + output.Replace("\"", "\"\"") + '"';

            return output;

        }
    }