close

Excel.Application objExcel_App = new Excel.Application();
Excel.Workbook objExcel_WB = (Excel.Workbook) objExcel_App.Workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
Excel.Worksheet objExcel_WS = objExcel_WB.Worksheets[1] as Excel.Worksheet;
Excel.Range objExcel_RG = null;

try
{
objExcel_WS.Name = "Test Report";

// 設定第一列Excel內容
ArrayList alRow = new ArrayList();
object[] objRow = {"A","B","C","D"};
alRow.Add(objRow);

objExcel_RG = objExcel_WS.get_Range("A1","D1");    
objExcel_RG.GetType().InvokeMember("Value",BindingFlags.SetProperty,null,objExcel_RG,
     alRow.ToArray(typeof(object)) as object[]);


// 設定Excel格式
objExcel_RG.Font.Bold = true;
objExcel_RG.Font.Name = "Arial";
objExcel_RG.Font.Size = 10;

objExcel_RG.Font.Color = 255; //字型顏色
objExcel_RG.Interior.Color =
System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); //背景顏色
objExcel_RG.VerticalAlignment = Excel.XlVAlign.xlVAlignTop; //垂直對齊
objExcel_RG.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平對齊
objExcel_RG.EntireRow.AutoFit(); //自動調整列高
objExcel_RG.EntireColumn.AutoFit(); //自動調整欄寬

objExcel_WS.get_Range("A1", "B1").Merge(false); //設定A1:B1儲存格合併
objExcel_WS.get_Range("C:C",Type.Missing).NumberFormatLocal
= "@";  //設定C欄儲存格格式為文字
objExcel_WS.get_Range("D:D",Type.Missing).NumberFormatLocal
= "yyyy/MM/dd";  //設定C欄儲存格格式


Array.Clear(objColumnTemp,0,objColumnTemp.Length);              

objExcel_WS.SaveAs(strFilePath,Type.Missing,Type.Missing,Type.Missing
           ,Type.Missing,Type.Missing,Type.Missing
                    ,Type.Missing,Type.Missing);
                    
objExcel_App.Workbooks.Close();

}
catch(Exception exp)
{
    throw exp;
}
finally
{
      objExcel_App.Quit();
      System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel_RG);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel_WS);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel_WB);
      System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel_App);
      GC.Collect();
}

objExcel_WS.get_Range(pricingUtil.ConvertToExcelColumn(iCol)+":"+pricingUtil.ConvertToExcelColumn(iCol),Type.Missing).NumberFormatLocal = "@";objExcel_WS.get_Range(pricingUtil.ConvertToExcelColumn(iCol)+":"+pricingUtil.ConvertToExcelColumn(iCol),Type.Missing).NumberFormatLocal = "@";
arrow
arrow
    全站熱搜

    felixhuang 發表在 痞客邦 留言(3) 人氣()