C#でEXCELファイルの出力

C#でEXCEL出力をしてみました。
VB+ADOのときのCopyFromRecordset はとても高速だったのに、かなり遅くなってしまった。

MSのサイトで最適化の余地ありだが。。。COMオブジェクト解放、テストで疲れたので。。。もう無理。。。

『Visual C# 2005 または Visual C# .NET を使用してデータを Excel ブックに転送する方法』
http://support.microsoft.com/kb/306023/ja



public static bool ExcelExports(string filePath, DataTable dataTable)
{
//レコード件数
int rowsCount = dataTable.Rows.Count;
int colsCount = dataTable.Columns.Count;

bool bRet;
try
{
//Excel開始
Excel.Application woApp = new Excel.Application();
woApp.DisplayAlerts = false;

try
{
// WorkBooksオブジェクト
Excel.Workbooks woBooks = woApp.Workbooks;

try
{
// Workook オプジェクト
Excel.Workbook woBook = null;

// ファイルが存在しているかどうか確認する
if (System.IO.File.Exists(filePath))
{
//' 既存の Excel ブックを開く
woBook = woBooks.Open(filePath,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
else
{
//Bookを追加
woBook = woBooks.Add(Type.Missing);
}


try
{
// Sheetsオブジェクト
Excel.Sheets woSheets = woBook.Worksheets;

try
{
//Sheetを追加
Excel.Worksheet woSheet = (Excel.Worksheet)woSheets.Add(
Type.Missing, Type.Missing, Type.Missing, Type.Missing);
try
{
//新規作成のとき、追加するシート以外を削除する
// ファイルが存在しているかどうか確認する
if (!System.IO.File.Exists(filePath))
{
// 余計なSheetを削除する
foreach (Excel.Worksheet sht in woSheets)
{
if (woSheet != sht) sht.Delete();

// COM オブジェクト解放
if (sht != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(sht);
}
}

//Sheet名
woSheet.Name = String.Format("{0:yyyyMMdd-HHmmss}", DateTime.Now);

//Cellsオブジェクト
Excel.Range woCells = (Excel.Range)woSheet.Cells;
try
{
//見出しデータ作成
int i = 1;
//列ループ
for (int j = 0; j < colsCount; j++)
{
woCells[i, j + 1] = dataTable.Columns[j].ColumnName;
}


//データの転記
//行ループ
for (int r = 0; r < rowsCount; r++)
{
//列ループ
for (int j = 0; j < colsCount; j++)
{
woCells[r + 2, j + 1] = dataTable.Rows[r][j];
}
}

//幅を文字列に合わせる
Excel.Range woColumns = woCells.Columns;
try
{
woColumns.AutoFit();
}
finally
{
// COM オブジェクト解放
if (woColumns != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woColumns);
}

//罫線をひく
string strLastAddress;
//最終セルの取得
Excel.Range woCellsLast = (Excel.Range)woCells[dataTable.Rows.Count + 1, dataTable.Columns.Count];
try
{
strLastAddress = woCellsLast.get_Address
(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing);
}
finally
{
// COM オブジェクト解放
if (woCellsLast != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woCellsLast);
}

//Rangeオブジェクト
Excel.Range woRange = woSheet.get_Range("A1", strLastAddress); //開始は、A1固定

try
{
//罫線をひく
Excel.Borders woBorders = woRange.Borders;
try
{
woBorders.LineStyle = Excel.XlLineStyle.xlContinuous; //外側および内側
}
finally
{
// COM オブジェクト解放
if (woBorders != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woBorders);
}
}
finally
{
// COM オブジェクト解放
if (woRange != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woRange);
}

// ファイルが存在しているかどうか確認する
if (System.IO.File.Exists(filePath))
{
//ファイルの上書き保存
woBook.Save();
}
else
{
//ファイルを「名前をつけて」保存する
woBook.SaveAs(filePath, Excel.XlFileFormat.xlWorkbookNormal,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
}
}
finally
{

// COM オブジェクト解放
if (woCells != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woCells);
}
}
finally
{
// COM オブジェクト解放
if (woSheet != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woSheet);
}
}
finally
{
// COM オブジェクト解放
if (woSheets != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woSheets);
}
}
finally
{
try
{
// 閉じる
woBook.Close(false, Type.Missing, Type.Missing);
}
finally
{
// COM オブジェクト解放
if (woBook != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woBook);
}
}
}
finally
{
try
{
// 閉じる
woBooks.Close();
}
finally
{
// COM オブジェクト解放
if (woBooks != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woBooks);
}
}
}
finally
{
try
{
//Excel終了
woApp.Quit();
}
finally
{
// COM オブジェクト解放
if (woApp != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(woApp);
}
}

bRet = true;
}
catch (Exception ex)
{
string errMsg = "Excel出力でエラーが発生しました。\n\n" + ex;
MessageBox.Show(errMsg);

bRet = false;
}

return bRet;
}
21 : 11 : 46 | プログラミング | トラックバック(0) | コメント(0) | page top↑
<<ACL4強!次は城南一和だ! | ホーム | SQL2005→SQL2000リストアエラー>>
コメント

コメントの投稿














管理者にだけ表示を許可する

トラックバック
トラックバックURL
http://playtoto.blog55.fc2.com/tb.php/31-7d352d76
この記事にトラックバックする(FC2ブログユーザー)
| ホーム |

カテゴリー

PR

デル株式会社

ジャパネットたかた メディアミックスショッピング

最近の記事

ブログ全記事表示

全ての記事を表示する

カレンダー

08 | 2012/09 | 10
- - - - - - 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 - - - - - -

検索フォーム

RSSフィード