Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否将excel格式的列表保存在本地驱动器中以供控制台应用程序使用?_C#_Asp.net_.net_Console_Export To Excel - Fatal编程技术网

C# 是否将excel格式的列表保存在本地驱动器中以供控制台应用程序使用?

C# 是否将excel格式的列表保存在本地驱动器中以供控制台应用程序使用?,c#,asp.net,.net,console,export-to-excel,C#,Asp.net,.net,Console,Export To Excel,我有一张单子。 我想以excel格式(.xls)保存列表,并将其保存到控制台应用程序中的本地驱动器 这是一个控制台应用程序。我想将它保存在一个位置 首先,我将列表转换为DataTable 列表项从Datatable创建Excel文件 将Excel保存在本地驱动器中的某个位置 代码片段 DataTable boundTable = Utils.ConvertToDataTable(MyList); /// <summary> /// Convert list to da

我有一张单子。 我想以excel格式(.xls)保存列表,并将其保存到控制台应用程序中的本地驱动器

这是一个控制台应用程序。我想将它保存在一个位置

  • 首先,我将列表转换为DataTable
  • 列表项从Datatable创建Excel文件
  • 将Excel保存在本地驱动器中的某个位置
  • 代码片段

    DataTable boundTable = Utils.ConvertToDataTable(MyList);
    /// <summary>
            /// Convert list to datatable
            /// </summary>
            /// <param name="data"></param>
            /// <returns></returns>
            public static DataTable ConvertToDataTable(IList<MyEntity> data)
            {
                PropertyDescriptorCollection properties =
                   TypeDescriptor.GetProperties(typeof(BDWData));
                DataTable table = new DataTable();
                foreach (PropertyDescriptor prop in properties)
                    table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
                foreach (BDWData item in data)
                {
                    DataRow row = table.NewRow();
                    foreach (PropertyDescriptor prop in properties)
                        row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
                    table.Rows.Add(row);
                }
                return table;
    
            }
    
    CreateXLSFile(boundTable, FileName);
    
    
    /// Create and Save Excel File from Datatable
            /// </summary>
            /// <param name="dt"></param>
            /// <param name="strFileName"></param>
            public void CreateXLSFile(DataTable dt, string strFileName)
            {
    
                #region Export List to XLS
    
                string date = DateTime.Now.ToString("MMddyyyyHHmmssfff");
                string path = strFileName + "_" + date + ".xls";
                if (dt.Rows.Count > 0)
                {
                    // Create a file to write to.
                    Directory.SetCurrentDirectory(ArchiveReportRoot);// Changed the default location of the storage of the file.
                    StreamWriter sw = File.AppendText(path); //Append the file name with the file path.
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("<TABLE Border=1 ID=\"TableH2\">");
                    sb.AppendLine("<TR>");
                    sb.Append("<TD colspan=\"" + dt.Columns.Count + "\" style=\"background-color:#88b2fe\" align=\"center\">"
                                                                + "<b> " + strFileName
                                                                + "</b>" + "</TD>");
                    sb.AppendLine("</TR>");
                    sb.AppendLine("<TR>");
                    for (int count = 0; count < dt.Columns.Count; count++)
                    {
                        sb.AppendLine("<TD style=\"background-color:#88b2fe\"><b>" +
                            dt.Columns[count].ColumnName + "</b></TD>");
                    }
                    sb.AppendLine("</TR>");
                    for (int count = 0; count < dt.Rows.Count; count++)
                    {
                        sb.AppendLine("<TR>");
                        DataRow dr = dt.Rows[count];
                        for (int cellCount = 0; cellCount < dt.Columns.Count; cellCount++)
                        {
                            sb.AppendLine("<TD style=\"mso-number-format:'\\@';\">" + dr[cellCount] + "</TD>");
                        }
                        sb.AppendLine("</TR>");
                    }
                    sb.AppendLine("<TR>");
                    for (int count = 0; count < dt.Columns.Count; count++)
                    {
                        sb.AppendLine("<TD style=\"background-color:#88b2fe\"><b>" + "</b></TD>");
                    }
    
                    sb.AppendLine("</TR>");
                    sb.AppendLine("</TABLE>");
                    sw.Write(sb.ToString());
                    sw.Write("\n");
                    sw.Close();
                }
    
                else
                {
                    #region Logging to the console and text file
    
                    //***** Logging - START *****
                    sb.Length = 0;
                    sb.Append(Utils.CurrentDateTime);
                    sb.Append("No BDW Records found on  " + Utils.CurrentDate);
                    if (Utils.IsLoggingEnabled)
                    {
                        Utils.LogException(sb.ToString(), LogListener.FireBackendListener);
                    }
                    Utils.WriteToConsole(sb.ToString());
    
                    //***** Logging - END *****
                    #endregion Logging to the console and text file
                }
    
                #endregion
    
            }
    
    DataTable boundTable=Utils.ConvertToDataTable(MyList);
    /// 
    ///将列表转换为数据表
    /// 
    /// 
    /// 
    公共静态数据表ConvertToDataTable(IList数据)
    {
    PropertyDescriptorCollection属性=
    GetProperties(typeof(BDWData));
    DataTable=新的DataTable();
    foreach(属性中的PropertyDescriptor属性)
    table.Columns.Add(prop.Name,Nullable.GetUnderlyingType(prop.PropertyType)??prop.PropertyType);
    foreach(数据中的BDWData项)
    {
    DataRow行=table.NewRow();
    foreach(属性中的PropertyDescriptor属性)
    行[prop.Name]=prop.GetValue(项)??DBNull.Value;
    table.Rows.Add(行);
    }
    返回表;
    }
    CreateXLSFile(绑定表,文件名);
    ///从Datatable创建并保存Excel文件
    /// 
    /// 
    /// 
    public void CreateXLSFile(DataTable dt,string strFileName)
    {
    #区域导出列表到XLS
    字符串日期=DateTime.Now.ToString(“mmddyyyyhmmssff”);
    字符串路径=strFileName+“”+date+“.xls”;
    如果(dt.Rows.Count>0)
    {
    //创建要写入的文件。
    Directory.SetCurrentDirectory(ArchiveReportRoot);//更改了文件存储的默认位置。
    StreamWriter sw=File.AppendText(path);//用文件路径追加文件名。
    StringBuilder sb=新的StringBuilder();
    某人加上一行(“”);
    某人加上一行(“”);
    某人附加(“)
    +“”+strFileName
    + "" + "");
    某人加上一行(“”);
    某人加上一行(“”);
    for(int count=0;count

    如果您有更好的选择,请告诉我。但以上代码适合我。

    Linq to Excel用于查询Excel电子表格。看看这个关于创建电子表格的问题。@Magnus我的错。实际上,我想在本地驱动器中导出一个列表(而不是刷新)…因为我在控制台应用程序中这样做。(我正在修改这个问题)