2011-08-08 56 views
5

Tôi đang viết một số dữ liệu từ cơ sở dữ liệu đến excel thông qua basic.net.I cần thay đổi nền của một số ô và cũng cần phải in đậm văn bản. Tôi cần một cái gì đó như thế:Thay đổi màu ô của bảng excel qua VB.NET

xlWorkSheet.Cells(rownumber, 1).BackgroundColor = Color.Yellow 
xlWorkSheet.Cells(rownumber, 1).Font.isBold = True 

Tất nhiên là không có công trình nào ở trên. Tôi có thể đạt được điều này như thế nào? Cảm ơn ..

Trả lời

9

Bạn cần phải tạo đối tượng Excel.Style và áp dụng đối tượng đó cho một dải ô. Như thế này:

Dim style As Excel.Style = xlWorkSheet.Application.ActiveWorkbook.Styles.Add("NewStyle") 
style.Font.Bold = True 
style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow) 

xlWorkSheet.Cells(rownumber, 1).Style = "NewStyle" 
+0

cách chúng tôi có thể thực hiện theo các chỉ mục như (1,1), (5,6), v.v ... không nằm trong khoảng A1, B1 ..? – dnur

+0

@dnur: xlWorkSheet.Cells (1,1) thuộc loại Range, vì vậy * Controls.AddNamedRange (xlWorkSheet.Cells (rownumber, 1), "rangeStyles") * sẽ hoạt động. –

+0

nó đưa ra một lỗi tại dòng có chứa "Globals.ThisWorkbook". Tôi đã thay đổi nó với tên sổ làm việc của tôi là "xlWorkBook" và "Globals.xlWorkBook" nhưng không ai trong số họ làm việc. Ngoài ra nó đưa ra lỗi thứ hai tại dòng có chứa "Microsoft.Office.Tools.Excel.NamedRange" và tôi đã thay đổi nó bằng "Microsoft.Office.Interop.Excel.ShapeRange". nó có đúng không – dnur

4

Điều này làm việc hoàn hảo cho tôi.

xlsWorkSheet.Cells (hàng, cột) .interior.color = Color.Green

+0

Rất đơn giản! –

1

Thats vài lời tuyên bố có thể giúp bạn cho phong cách Excel
Đối với bảng màu: http://dmcritchie.mvps.org/excel/colors.htm

   Dim xlsCell As Excel.Range 
        xlsCell = xlWorkSheet.Range("A1") 
        xlsCell.Range("A5").Value = "TEXT" 

        With xlsCell.Range("A12:J12") 
         .Merge() 
         .Borders(XlBordersIndex.xlEdgeBottom).Weight = 2 
         .Borders(XlBordersIndex.xlEdgeTop).Weight = 2 
         .Borders(XlBordersIndex.xlEdgeLeft).Weight = 2 
         .Borders(XlBordersIndex.xlEdgeRight).Weight = 2 
         .Borders(XlBordersIndex.xlInsideHorizontal).Weight = 2 
         .Borders(XlBordersIndex.xlInsideVertical).Weight = 2 
         .Interior.ColorIndex = 15       
         .WrapText = True 
         .Font.FontStyle = "Arial" 
         .VerticalAlignment = Excel.XlHAlign.xlHAlignCenter 
         .HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft 
        End With 
0
void SetCaptionStyle(ExcelStyle style) 
    { 
     style.Fill.PatternType = ExcelFillStyle.Solid; 
     style.Fill.BackgroundColor.SetColor(Color.FromArgb(184, 204, 228)); 

    } 
+1

Bạn có thể giải thích cách nhập và gọi hàm C# này từ VB.NET không? – Tino

+0

Tôi mới sử dụng ngôn ngữ này. Nhưng tôi tìm thấy một số URL giúp đỡ. Hy vọng rằng sẽ giúp bạn. 1) http://vb.net-informations.com/excel-2007/vb.net_excel_page_format.htm 2) https://www.gemboxsoftware.com/spreadsheet/examples/c-sharp-vb-net-excel kiểu-định dạng/202 – Jagdeep

Các vấn đề liên quan