4

Excel レポートの作成に Gembox という外部コンポーネントを使用しています。

C# の DateTime であるプロパティを Excel にエクスポートする場合、値は Excel では数値と​​して表示されます (たとえば、'-693593')。他のすべての (基本的な) プロパティ タイプは、 が表すタイプとして表示されます。

Excel では、DateTime を適切な日付として表示したいのですが、Gembox オプションで、形式を日付に設定するオプションが見つかりませんでした。

別のオプションは、Excel で値を設定しながら .ToString() で DateTime を変換することです。これにより、目的の結果が得られますが、より良い方法はありますか?

簡単なコード例:

ExcelFile excelFile = new ExcelFile();
ExcelWorksheet excelWorksheet = excelFile.Worksheets.Add("MyWorksheet");

int rownumber = 0;
int columnnumber = 2;
DateTime dateTime = DateTime.Now;

excelWorksheet.Cells[rownumber, columnnumber].Value = dateTime;

出力例:

Excelエクスポート例

4

3 に答える 3

2

Excelセルをフォーマットして、Excel自体と同じように日時を表示します。

ExcelFile excelFile = new ExcelFile();
ExcelWorksheet excelWorksheet = excelFile.Worksheets.Add("MyWorksheet");

int rownumber = 0;
int columnnumber = 2;
DateTime dateTime = DateTime.Now;

Microsoft.Office.Interop.Excel.Range range = excelWorksheet.Cells[rownumber, columnnumber] as Range;
range.NumberFormat = "dd/MMM/yyyy";
range.Value2 = dateTime;

これにより、Excel セルが日付形式に設定されます。

行の中 range.NumberFormat = "dd/MMM/yyyy"; 以下の形式のいずれかを指定できます。

dd/MM/yy         // 01/01/01
dd/MMM/yy         // 01/Jan/01
ddd/MMM/yyyy      // Mon/Jan/2001
ddd dd/MMM/yyyy   // Mon 01/Jan/2001
hh:mm:ss          // 01:01:01  - 24 hour
hh:mm:ss AM/PM    // 01:01:01 pm - 12 hour

the format below is for used by the .Net format only, this will not work with Excel.
HH:mm:ss          // 01:01:01  - 24 hour
hh:mm:ss t        // 01:01:01 pm - 12 hour
HH:mm:ss.fff      // 01:01:01.123  - 24 hour with millisecond


or combination of both date and time;
ddd dd/MMM/yyyy hh:mm:ss
于 2013-05-16T10:16:46.200 に答える