輸出Excelファイルによって生成されたC#

#region生成エクセル文件
パブリック静的ボイドExportExcel(DTたSystem.Data.DataTable、ストリングstrFile、列strMailCode)
{
LogTool LG =新しいLogTool()。
lg.Write( "获取DT记录数:" + dt.Rows.Count)。
もし(!DT = NULL)
{
Microsoft.Office.Interop.Excel.Applicationエクセル=新しいMicrosoft.Office.Interop.Excel.Application();

lg.Write( "オープンExcelファイル...");
IF(エクセル== NULL){リターン;}
excel.Visible = falseに、セット見えない、操作がバックグラウンドで実行される//、それは真のオープンエクセルであります

//excel.DisplayFullScreen =真;全開の設定//明示的なとき
Microsoft.Office.Interop.Excel.Workbooksワークブック= excel.Workbooks; //初期化ワークブック

//新しいブックを追加し、追加()メソッドは、直接パラメータをtrueに渡すことができる
Microsoft.Office.Interop.Excel.Workbookワークブック= Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
//同じこれは、新しいワークブックですが、名前を付けて保存]ダイアログボックスがポップアップ表示されます
(真)//Microsoft.Office.Interop.Excel.Workbookワークブック= excel.Application.Workbooks.Add。

//新しいExcelシートを追加します(シート)
Microsoft.Office.Interop.Excel.Worksheetワークシート=(Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets [1];
worksheet.Name = dt.TableName; //セットテーブルの名前
試み
{
Microsoft.Office.Interop.Excel.Range範囲; //セル作成
INT rowIndexプロパティ= 1; //行のインデックスを開始1。
INT = colIndex ,. 1; //列インデックスを開始1

用(INT iは= 0; I <dt.Columns.Count; I ++)// セット列名
{
//設定された第1行、即ち、カラム名
worksheet.Cells [rowIndexに、colIndex + I] = dt.Columns [I] .ColumnName;
//最初の行の各セルを取得
範囲=(Microsoft.Office.Interop.Excel.Range)worksheet.Cells [rowIndexに、colIndex、+ I];
//はセル内部のカラー設定
range.Interiorを。 = 0のColorIndex; // 16 // 48 // 15
range.Font.Bold = falseに; //太字
range.Font.Color = 0;黒//セット
range.Font.Name = "Calibri"; / /フォントセット
range.Font.Size = 10; //設定されたフォントサイズ
range.Horizo​​ntalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //水平中心
range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign。 xlVAlignCenter; //垂直中心
range.ColumnWidth = 12; //列幅を設定
}

IF(strMailCode == "103")
{
ため(INT 6 = Y; Y <dt.Columns.Count; Y ++)
{
worksheet.Cells [rowIndexに、colIndex、+ Y] = dt.Columns [Y] .ColumnName; //最初の行、すなわち、列名は、
範囲=(Microsoft.Office.Interop.Excel.Range)worksheet.Cells [rowIndexに 、colIndex + Y]; // 最初の行の各グリッドセルを取得
range.Interior.ColorIndex = 17 ; // 17 // 20セルの色の内側に設けられ
range.ColumnWidth = 24;カラム幅を設定//
; range.Font.Color = 2
}
}

rowIndexプロパティ++を; //最初の行をスキップし、最初の行が書き込まれ列名

//写入数据
ため(INT I = 0、I <dt.Rows.Count; I ++)
{
ため(INT J = 0; J <dt.Columns.Count; J ++)
{
worksheet.Cells [rowIndexに+ I、colIndex + J] = dt.Rows [I] [J] .ToString();
}
}
// ********************************************** ********************************************
worksheet.Rows.RowHeight = 16;
worksheet.Rows.Font.Size = 10。
worksheet.Rows.Font.Name = "Calibri"。
worksheet.Rows.Horizo​​ntalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //水平居中
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows [ "1:1"、System.Type.Missing] ).RowHeight = 24; //表头行高

// ************************************************ ******************************************
場合(strMailCode == "101" )
{
worksheet.get_Range(worksheet.Cells [1,3]、worksheet.Cells [dt.Rows.Count + 1、3])columnWidthの= 9。
worksheet.get_Range(worksheet.Cells [1,4]、worksheet.Cells [dt.Rows.Count + 1、4])columnWidthの= 10。
worksheet.get_Range(worksheet.Cells [1、2]、worksheet.Cells [dt.Rows.Count + 1、2])=たHorizo​​ntalAlignment Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
worksheet.get_Range (worksheet.Cells [1,4]、worksheet.Cells [dt.Rows.Count + 1、4])=たHorizo​​ntalAlignment Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
worksheet.get_Range(worksheet.Cells [1]、[12]、worksheet.Cells [dt.Rows.Count + 1、12])=たHorizo​​ntalAlignment Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
worksheet.get_Range (worksheet.Cells [1、13]、worksheet.Cells [dt.Rows.Count + 1、13])=たHorizo​​ntalAlignment Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
worksheet.get_Range(ワークシート。細胞を[1、14]、worksheet.Cells [dt.Rows.Count + 1、14])=たHorizo​​ntalAlignment Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
用(INT Y = 6; Y < = 11; Y ++)
{
worksheet.get_Range(worksheet.Cells [1、y]は、worksheet.Cells [dt.Rows.Count + 1、Y])columnWidthの= 8。
}
worksheet.get_Range(worksheet.Cells [1]、[12]、worksheet.Cells [dt.Rows.Count + 1、12])columnWidthの= 18。
worksheet.get_Range(worksheet.Cells [1、13]、worksheet.Cells [dt.Rows.Count + 1、13])columnWidthの= 18。
worksheet.get_Range(worksheet.Cells [1、14]、worksheet.Cells [dt.Rows.Count + 1、14])columnWidthの= 18。
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows [ "1:1"、System.Type.Missing])Font.Bold = TRUE;。
}

IF(strMailCode == "102")
{
ため(INT 4 = Y; Y <= dt.Columns.Count; Y ++)
{
範囲=(Microsoft.Office.Interop.Excel.Range)worksheet.Cells [1、Y。] ; //最初の行の各グリッドセルを取得
range.Interior.ColorIndex = 15; //セルの色の内側に設けられ// 20 17
range.Horizo​​ntalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter; //水平中心
}

lg.Write( "エクセル第一列合并单元格....");
文字列strBeginArea = "";
(iは1 = INT; I <dt.Rows.Count + 1; I ++)用
{
文字列TEMP =((Microsoft.Office.Interop.Excel.Range)worksheet.Cells [I、1])Text.ToString()。 。トリム();
(strBeginArea ==温度)であれば
{
Microsoft.Office.Interop.Excel.Range colMer = worksheet.get_Range(worksheet.Cells [I - 1、1]、worksheet.Cells [I、1])。
colMer.Application.DisplayAlerts = falseは、
colMer.Merge(System.Type.Missing)。
colMer.Application.DisplayAlerts =はtrue。
}
strBeginArea = TEMP。
}
worksheet.get_Range(worksheet.Cells [1,1]、worksheet.Cells [dt.Rows.Count + 1、1])columnWidthの= 10。
worksheet.get_Range(worksheet.Cells [1、2]、worksheet.Cells [dt.Rows.Count + 1、2])columnWidthの= 18。
worksheet.get_Range(worksheet.Cells [1、2]、worksheet.Cells [dt.Rows.Count + 1、2])=たHorizo​​ntalAlignment Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
((マイクロソフト.Office.Interop.Excel.Range)worksheet.Rows [ "1:1"、System.Type.Missing]。)Font.Bold = TRUE。
}
(strMailCode == "104")であれば
{
((Microsoft.Office.Interop.Excel.Range)worksheet.Rows [ "1:1"、System.Type.Missing])Font.Bold = TRUE;。
worksheet.get_Range(worksheet.Cells [2,1]、worksheet.Cells [dt.Rows.Count + 1、1])NumberFormatLocal = "YYYY-MM"。
worksheet.get_Range(worksheet.Cells [2,2]、worksheet.Cells [dt.Rows.Count + 1、2])。NumberFormatLocal = "
。worksheet.get_Range(worksheet.Cells [1,1]、worksheet.Cells [dt.Rows.Count + 1、1])columnWidthの= 8。
。worksheet.get_Range(worksheet.Cells [1、2]、worksheet.Cells [dt.Rows.Count + 1、2])columnWidthの= 8。
worksheet.get_Range(worksheet.Cells [1,3]、worksheet.Cells [dt.Rows.Count + 1、3])columnWidthの= 32。
。worksheet.get_Range(worksheet.Cells [1,8]、worksheet.Cells [dt.Rows.Count + 1,8])columnWidthの= 6。
。worksheet.get_Range(worksheet.Cells [1、13]、worksheet.Cells [dt.Rows.Count + 1、13])columnWidthの= 9。
worksheet.get_Range(worksheet.Cells [1,3]、worksheet.Cells [dt.Rows.Count + 1、3])たHorizo​​ntalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
worksheet.get_Range(worksheet.Cells [1,5]、worksheet.Cells [dt.Rows.Count + 1、5])たHorizo​​ntalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
worksheet.get_Range (worksheet.Cells [1,7]、worksheet.Cells [dt.Rows.Count + 1,7])たHorizo​​ntalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignRight; //水平居右
worksheet.get_Range(ワークシート.Cells [1,9]、worksheet.Cells [dt.Rows.Count + 1,9])たHorizo​​ntalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
worksheet.get_Range(worksheet.Cells [ 1、10]、worksheet.Cells [dt.Rows.Count + 1、10])=たHorizo​​ntalAlignment Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
worksheet.get_Range(worksheet.Cells [1、11]、worksheet.Cells [dt.Rows.Count + 1、11])=たHorizo​​ntalAlignment Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
worksheet.get_Range (worksheet.Cells [1]、[12]、worksheet.Cells [dt.Rows.Count + 1、12])=たHorizo​​ntalAlignment Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft; //水平居左
//Microsoft.Office。 Interop.Excel.Range colMer = worksheet.get_Range(worksheet.Cells [2,1]、worksheet.Cells [dt.Rows.Count + 1、1])。
//colMer.NumberFormatLocal = "YYYY-MM"。
}

//あなたはシートのページを削除したい場合は、このセットfasleへのすべての最初の、プロンプトが表示された場合。
= falseをexcel.DisplayAlerts;
workbook.Saved = trueに; //書き込まれたデータの保存、ディスクにそこに保存されていない
//ファイルの作成
のFileStreamファイルを=新しいFileStreamを(strFile、 FileMode.CreateNew)。

//近い放出流、または書き込みデータに決して
File.close();
file.Disposeを();

//指定されたパスに保存
workbook.SaveCopyAs(strFile);
}
キャッチ(例外ERR)
{
lg.Write( "\ n型間違った:" + err.message);
}
最後に
{
workbook.Close(falseに、Type.Missing 、Type.Missing);
workbooks.Close();
excel.Quit(); //近い出口

ワークシート= NULL;
ワークブック= NULL;
ワークブック= NULL;
= nullのエクセル。

GC.Collect();
}
}
}
#endregion

おすすめ

転載: www.cnblogs.com/812FC/p/12167602.html