2017-06-05 78 views
-6

我有上述方法中,其中i傳遞四個參數,等的方法,如何在Excel中插入單行數據在c#中?

public void loggeneration(DateTime datetime, string projectname, int totallines,int sum,int max) 
{ 
} 

,我要創建一個excel表(如果文件是存在已經,只是追加的最後一個空白行中的數據)並將數據(日期時間,項目名稱,總計,總和,最大值)插入一行五列中。

第一行默認情況下應該是列名。

我已經盡力了,找不到確切的解決方案。有人可以提供解決方案嗎?在此先感謝

+0

這裏有一個很好的例子,讓你開始[鏈接](https://stackoverflow.com/questions/10100901/creating-simple-excel-sheet-in-c-sharp-with-strings- as-input) – stuicidle

+0

[在c#中用字符串作爲輸入創建簡單的Excel表單]的可能的副本(https://stackoverflow.com/questions/10100901/creating-simple-excel-sheet-in-c-sharp-with-strings -as-input) – jAC

回答

0

確實,這個問題已經存在堆棧溢出。順便說一下,你可以試試這個簡單的解決方案

public void loggeneration(DateTime datetime, string projectname, int totallines, int sum, int max) 
    { 
     // this is the variable to your xls file 
     string path = @"c:\temp\log.xls"; 

     // This text is added only once to the file. 
     if (!File.Exists(path)) 
     { 
      // Create a file to write to. 
      using (StreamWriter sw = File.CreateText(path)) 
      { 
       //once file was created insert the columns 
       sw.WriteLine("date;projectname;totallines;sum;max"); 

      } 
     } 
     // This text is always added, making the file longer over time 
     using (StreamWriter sw = File.AppendText(path)) 
     { 
      sw.WriteLine(String.Format("{0};{1};{2};{3};{4}", datetime, projectname, totallines, sum, max)); 
     } 
    } 
+0

謝謝你的回答。整個數據輸入到一個單元格中。你有什麼想法將數據插入到表格的單獨單元中 – Annamalai

+0

嗨,非常歡迎。 對不起我的錯誤,使用昏迷分隔值擴展而不是xls。 替換: string path = @「c:\ temp \ log.xls」;對於 string path = @「c:\ temp \ log.csv」; 這可能工作 祝你好運! –

+0

感謝您的解決方案 – Annamalai