How to use cell format for numbers while writing spreadsheet document using Bytescout Spreadsheet SDK
This source code sample demonstrates how to use different number formatting for cells in spreadsheet generated with Bytescout Spreadsheet SDK
Download example source code: bytescoutxls_number_formatting_for_cells.zip (8 KB)
Generated spreadsheet screenshot:
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Bytescout.Spreadsheet;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// Create new Spreadsheet
Spreadsheet document = new Spreadsheet();
// Get worksheet by name
Worksheet worksheet = document.Workbook.Worksheets.Add(“Sample”);
// Set header
worksheet.Cell(0, 0).Value = “Number”;
worksheet.Cell(0, 1).Value = “Format”;
// Set variables
double plan = 3000;
double result = 3100;
// Fill cells
for (int i = 1; i < 6; i++)
{
worksheet.Cell(i, 0).Value = result / plan;
}
// Set cells format
worksheet.Cell(1, 0).NumberFormat = “0.00”;
worksheet.Cell(2, 0).NumberFormat = “0.00%”;
worksheet.Cell(3, 0).NumberFormat = “mm:ss”;
worksheet.Cell(4, 0).NumberFormat = “hh:mm:ss”;
worksheet.Cell(5, 0).NumberFormat = “##0,0E+0”;
// Set captions
worksheet.Cell(1, 1).Value = “0.00”;
worksheet.Cell(2, 1).Value = “0.00%”;
worksheet.Cell(3, 1).Value = “mm:ss”;
worksheet.Cell(4, 1).Value = “hh:mm:ss”;
worksheet.Cell(5, 1).Value = “##0,0E+0”;
// Save document
document.SaveAs(“Sample.xls”);
// Close document
document.Close();
// open generated XLS document in default program
Process.Start(“Sample.xls”);
}
}
}
Download example source code: bytescoutxls_number_formatting_for_cells.zip (8 KB)