2017-06-05 2 views
-6

Iは、上記の方法の中C#でExcelの単一行データを挿入するには?

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

、同様に、私は、4つのパラメータを渡す方法を有する(ファイルが既に存在する場合だけ、最後の空白行のデータを追加、)、iはExcelシートを作成する必要が5つの列の行にデータ(datetime、projectname、totallines、sum、max)を挿入します。

最初の行はデフォルトで列名にする必要があります。

私は最善を尽くしましたが、正確な解決策を見つけることができませんでした。誰かが解決策を提供できますか?おかげでアドバンス

+0

ここに良い例では、[リンク](https://stackoverflow.com/questions/10100901/creating-simple-excel-sheet-in-c-sharp-with-strings-を始めるためにあり入力として) – stuicidle

+0

[文字列を入力として単純な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の代わりに昏睡区切りの値の拡張子を使用してください。 置き換えます: 文字列パス= @ "c:\ temp \ log.xls"; string path = @ "c:\ temp \ log.csv"; これが動作する可能性があります 幸運! –

+0

ソリューションをありがとう – Annamalai

関連する問題