此代碼通過讀取文本文件內容,處理其中的數據並將輸出顯示在文本框中起作用。在C#ASP.Net中顯示來自文本文件的空值或零值數據
using (StreamReader stRead = new StreamReader(FileUpload1.PostedFile.InputStream))
{
string filenameDate = FileUpload1.FileName.Substring(15, 2);
Dictionary<string, int> dMyobject = new Dictionary<string, int>();
while (!stRead.EndOfStream)
{
var readedLine = stRead.ReadLine();
if (!string.IsNullOrWhiteSpace(readedLine))
{
//int readedLineTime = Convert.ToInt32(readedLine.Substring(09, 02));
string sDate = readedLine.Substring(0, 11);
MatchCollection collection = Regex.Matches(readedLine, @"D;");
countedChars = collection.Count;
if (!dMyobject.Keys.Contains(sDate))
{
dMyobject.Add(sDate, collection.Count);
}
else
{
dMyobject[sDate] = dMyobject[sDate] + collection.Count;
}
}
textfileContent += readedLine + Environment.NewLine;
i++;
}
txtContent.Text = textfileContent;
lblLineCount.Text = i.ToString();
//Label1.Text = this.TextBox1.Text.Split(new Char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).Length.ToString();
lblFileDate.Text = filenameDate;
foreach (var item in dMyobject)
{
textfileOutput += (item.Key + " " + item.Value) + Environment.NewLine;
// textfileOutput += (item.Value) + Environment.NewLine;
}
txtOutput.Text = textfileOutput;
}
textfile中的每一行都包含一個日期和一系列模式。此代碼讀取由日期分隔的每行並計算模式的總髮生次數,在本例中,計數模式「D」。並總結其日期總數(相同日期的行應彙總)。例如
運行代碼會給這個電流輸出
2016-12-01 - 7
2016-12-02 - 9
2016-12-03 - 5
2016-12-05 - 1
我的目標和期望輸出的輸出應包括與NULL值的日期。在我的例子,有沒有2016年12月4日的記錄,預計**輸出應該仍然顯示日期和顯示0或空白,如:**
2016-12-01 - 7
2016-12-02 - 9
2016-12-03 - 5
2016-12-04 - 0
2016-12-05 - 1
你是對的,道歉,請看我編輯的問題@johanP – rickyProgrammer
更新了我的回答 – JohanP
感謝這個,但唯一可能缺少的是如果例如,第一個日期爲空,dec 1爲空,它不會顯示它 – rickyProgrammer