Independentsoft
- any library, any programming language
Home
Purchase
Support
Company
Contact
Spreadsheet .NET
>
Tutorial
> Set Cell Format
The following example shows you how to set cell format.
C# example
using System; using Independentsoft.Office; using Independentsoft.Office.Spreadsheet; using Independentsoft.Office.Spreadsheet.Styles; namespace Sample { class Program { static void Main(string[] args) { Workbook book = new Workbook(); MasterCellFormat commaFormat = new MasterCellFormat(); commaFormat.NumberFormat = new NumberFormat(1, "#,##0.00"); MasterCellFormat currencyFormat = new MasterCellFormat(); currencyFormat.NumberFormat = new NumberFormat(2, "$#,##0.00"); book.Styles.MasterCellFormats.Add(commaFormat); book.Styles.MasterCellFormats.Add(currencyFormat); CellFormat format1 = new CellFormat(); format1.NumberFormatID = 1; CellFormat format2 = new CellFormat(); format2.NumberFormatID = 2; Cell a1 = new Cell(9999.99); a1.Format = format1; Cell a2 = new Cell(9999.99); a2.Format = format2; Worksheet sheet1 = new Worksheet(); sheet1["A1"] = a1; sheet1["A2"] = a2; book.Sheets.Add(sheet1); book.Save("c:\\test\\output.xlsx", true); } } }
VB example
Imports System Imports Independentsoft.Office Imports Independentsoft.Office.Spreadsheet Imports Independentsoft.Office.Spreadsheet.Styles Module Module1 Sub Main(ByVal args As String()) Dim book As New Workbook() Dim commaFormat As New MasterCellFormat() commaFormat.NumberFormat = New NumberFormat(1, "#,##0.00") Dim currencyFormat As New MasterCellFormat() currencyFormat.NumberFormat = New NumberFormat(2, "$#,##0.00") book.Styles.MasterCellFormats.Add(commaFormat) book.Styles.MasterCellFormats.Add(currencyFormat) Dim format1 As New CellFormat() format1.NumberFormatID = 1 Dim format2 As New CellFormat() format2.NumberFormatID = 2 Dim a1 As New Cell(9999.99) a1.Format = format1 Dim a2 As New Cell(9999.99) a2.Format = format2 Dim sheet1 As New Worksheet() sheet1("A1") = a1 sheet1("A2") = a2 book.Sheets.Add(sheet1) book.Save("c:\test\output.xlsx", True) End Sub End Module
Need help? Ask our developers:
Name*
Email*
Message*