下面就是設定框線及字體對齊的語法

private void button1_Click(object sender, EventArgs e)
{
Excel._Application myExcel = null;
Excel._Workbook myBook = null;
Excel._Worksheet mySheet = null;
try
{
myExcel = new Excel.Application();    //開啟一個新的應用程式
myExcel.DisplayAlerts = false;        //停用警告訊息
myBook = myExcel.Workbooks.Add(true); //新增活頁簿
mySheet = (Excel._Worksheet)myBook.Worksheets[1];//引用第一張工作表
myExcel.Visible = true;               //顯示Excel程式

mySheet.Cells.Font.Name = "標楷體";   //設定Excel資料字體字型
mySheet.Cells.Font.Size = 20;        //設定Excel資料字體大小

//Excel寫入資料
mySheet.Cells[1, 1] = "1";
mySheet.Cells[1, 2] = "A";
mySheet.Cells[1, 3] = "ㄅ";

mySheet.Cells[2, 1] = "2";
mySheet.Cells[2, 2] = "B";
mySheet.Cells[2, 3] = "ㄆ";

mySheet.Cells[3, 1] = "3";
mySheet.Cells[3, 2] = "C";
mySheet.Cells[3, 3] = "ㄇ";

mySheet.Cells[4, 1] = "4";
mySheet.Cells[4, 2] = "D";
mySheet.Cells[4, 3] = "ㄈ";

//設定框線
string Range = "A1:C4";  //設定範圍
mySheet.get_Range(Range).Borders.LineStyle = Excel.XlLineStyle.xlContinuous;

//設定對齊方式
//靠左
string RangeLeft = "A1:A4";//設定範圍
mySheet.get_Range(RangeLeft).HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
//靠右
string RangeRight = "B1:B4";//設定範圍
mySheet.get_Range(RangeRight).HorizontalAlignment = Excel.XlHAlign.xlHAlignRight;
//置中
string RangeCenter = "C1:C4";//設定範圍
mySheet.get_Range(RangeCenter).HorizontalAlignment=Excel.XlHAlign.xlHAlignCenter;
}
catch (Exception)
{
myExcel.Visible = true;
}
finally
{
    //把執行的Excel資源釋放
    System.Runtime.InteropServices.Marshal.ReleaseComObject(myExcel);
    myExcel = null;
    myBook = null;
    mySheet = null;
}
}

 

Excel畫面結果

Excel對齊.jpg

 

 

 

 

 

 

 

 

 

arrow
arrow
    文章標籤
    C# Excel
    全站熱搜
    創作者介紹
    創作者 熊仔 的頭像
    熊仔

    熊仔的部落格

    熊仔 發表在 痞客邦 留言(0) 人氣()