0

i'm trying to convert a DataTable to an excel-file but at the time of creation of file it gives me error: could not find part of path.

string strDirectorypath = HttpContext.Current.Server.MapPath("~/ExcelUpload/");
DateTime dtbatchtime = Convert.ToDateTime(strBatchProcesstime);
strBatchProcesstime = dtbatchtime.ToString("MM-dd-yyyy_hh_mm_ss");

string strFilename = "FailedExcel_" + strBatchProcesstime + ".csv";
string csvdownloadPath = Path.Combine(strDirectorypath, strFilename);
using (FileStream File_Stream = new FileStream(csvdownloadPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
    using (StreamWriter FileWriter = new StreamWriter(File_Stream))
    {
        FileWriter.BaseStream.Seek(0, SeekOrigin.End);
        string[] columns = { "Opportunity Name", "Property Id", "Error Type", "Error Message", "Time" };//set columns here as in the Excel Sheet.
        CreateColumns(FileWriter, columns);
        FileWriter.WriteLine();
        if (dtRecords.Rows.Count > 0)
        {
            for (int i = 0; i < dtRecords.Rows.Count; i++)
            {
                string[] values = { 
                    dtRecords.Rows[i]["OpportunityName"].ToString(), 
                    dtRecords.Rows[i]["PropertyId"].ToString(), 
                    dtRecords.Rows[i]["ErrorType"].ToString(), 
                    dtRecords.Rows[i]["ErrorMessage"].ToString(), 
                    dtRecords.Rows[i]["TimeStamp"].ToString() 
                };
                CreateColumns(FileWriter, values);
                FileWriter.WriteLine();
            }
        }

    }
}
4

1 に答える 1

0

ディレクトリが存在しない場合は作成されないため、確認して作成する必要があります。

if (!Directory.Exists(strDirectorypath)) {
  Directory.Create(strDirectorypath);
}

あなた自身の信頼性/正気をある程度チェックして、それを拡張してください。

于 2013-02-22T11:46:11.037 に答える