1. 程式人生 > 實用技巧 >Go語言學習筆記四--基本資料型別與string型別常用方法

Go語言學習筆記四--基本資料型別與string型別常用方法

1、公式計算

worksheet.Cells["D2:D5"].Formula = "B2*C2";//這是乘法的公式,意思是第二列乘以第三列的值賦值給第四列,這種方法比較簡單明瞭
worksheet.Cells[6, 2, 6, 4].Formula = string.Format("SUBTOTAL(9,{0})", new ExcelAddress(2, 2, 5, 2).Address);//這是自動求和的方法,至於subtotal的用法你需要自己去了解了

2、設定單元格格式

worksheet.Cells[5, 3].Style.Numberformat.Format = "#,##0.00";//這是保留兩位小數

3、設定字型和單元格樣式

設定單元格對齊方式
worksheet.Cells[1, 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;//水平居中
worksheet.Cells[1, 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;//垂直居中
worksheet.Cells[1, 4, 1, 5].Merge = true;//合併單元格
worksheet.Cells.Style.WrapText = true;//自動換行
設定單元格字型樣式

worksheet.Cells[1, 1].Style.Font.Bold = true;//字型為粗體

worksheet.Cells[1, 1].Style.Font.Color.SetColor(Color.White);//字型顏色
worksheet.Cells[1, 1].Style.Font.Name = "微軟雅黑";//字型
worksheet.Cells[1, 1].Style.Font.Size = 12;//字型大小
設定單元格背景樣式

worksheet.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(128, 128, 128));//設定單元格背景色

設定單元格邊框,兩種方法

worksheet.Cells[1, 1].Style.Border.BorderAround(ExcelBorderStyle.Thin, Color.FromArgb(191, 191, 191));//設定單元格所有邊框
worksheet.Cells[1, 1].Style.Border.Bottom.Style = ExcelBorderStyle.Thin;//單獨設定單元格底部邊框樣式和顏色(上下左右均可分開設定)
worksheet.Cells[1, 1].Style.Border.Bottom.Color.SetColor(Color.FromArgb(191, 191, 191));
設定單元格的行高和列寬

worksheet.Cells.Style.ShrinkToFit = true;//單元格自動適應大小
worksheet.Row(1).Height = 15;//設定行高
worksheet.Row(1).CustomHeight = true;//自動調整行高
worksheet.Column(1).Width = 15;//設定列寬

4、設定sheet背景

worksheet.View.ShowGridLines = false;//去掉sheet的網格線
worksheet.Cells.Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells.Style.Fill.BackgroundColor.SetColor(Color.LightGray);//設定背景色
worksheet.BackgroundImage.Image = Image.FromFile(@"firstbg.jpg");//設定背景圖片
5、插入圖片和形狀

插入圖片

ExcelPicture picture = worksheet.Drawings.AddPicture("logo", Image.FromFile(@"firstbg.jpg"));//插入圖片
picture.SetPosition(100, 100);//設定圖片的位置
picture.SetSize(100, 100);//設定圖片的大小
插入形狀

ExcelShape shape = worksheet.Drawings.AddShape("shape", eShapeStyle.Rect);//插入形狀
shape.Font.Color = Color.Red;//設定形狀的字型顏色
shape.Font.Size = 15;//字型大小
shape.Font.Bold = true;//字型粗細
shape.Fill.Style = eFillStyle.NoFill;//設定形狀的填充樣式
shape.Border.Fill.Style = eFillStyle.NoFill;//邊框樣式
shape.SetPosition(200, 300);//形狀的位置
shape.SetSize(80, 30);//形狀的大小
shape.Text = "test";//形狀的內容
6、超連結

給圖片加超連結

ExcelPicture picture = worksheet.Drawings.AddPicture("logo", Image.FromFile(@"firstbg.jpg"), new ExcelHyperLink("http:\\www.baidu.com", UriKind.Relative));
給單元格加超連結

worksheet.Cells[1, 1].Hyperlink = new ExcelHyperLink("http:\\www.baidu.com", UriKind.Relative);

7、隱藏sheet

worksheet.Hidden = eWorkSheetHidden.Hidden;//隱藏sheet
worksheet.Column(1).Hidden = true;//隱藏某一列
worksheet.Row(1).Hidden = true;//隱藏某一行