職業プログラマの備忘録です。 SQLServer,Oracle,DB2,C#,VB.net,VB6.0,Excel,Java,Eclipse,IIS,Apache,Tomcat,RUBY,rails,c,c++ totoで億万長者を夢見つつ、lotoも買ってたりします。 クロスバイク通勤してます。フルマラソンでサブフォーが目標です。
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;
}
| 日 | 月 | 火 | 水 | 木 | 金 | 土 |
|---|---|---|---|---|---|---|
| - | - | - | - | - | - | 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 | - | - | - | - | - | - |