How to create a XLS document using C# and .NET

Step-by-step guide:

  1. Add SautinSoft.Excel from Nuget.
  2. Create a new document.
  3. Add a worksheet.
  4. Create a variable to address.
  5. Save to a XLS format.

Полный код

using SautinSoft.Excel;
using SkiaSharp;
using System;
using System.Text;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free key here:   
            // https://sautinsoft.com/start-for-free/

            CreateExcelDocument();
        }

        /// <summary>
        /// Creates a new XLS document.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-xls-document-net-csharp-vb.php
        /// </remarks>

        static void CreateExcelDocument()
        {
            // Set a path to our Document
            string outFile = @"..\..\..\Result.xls";

            // Create a new document
            ExcelDocument excelDocument = new ExcelDocument();

            // Add several worksheets
            excelDocument.Worksheets.Add("The main worksheet");
            excelDocument.Worksheets.Add("Second worksheet");

            // Create a variable to address
            var worksheet = excelDocument.Worksheets["The main worksheet"];

            // Add plain text
            worksheet.Cells["A1"].Value = "This is common string";
            worksheet.Cells["B1"].Value = "Hello, World! 12345";

            // Add the result of  the expression
            worksheet.Cells["A2"].Value = "This is the result of a mathematical expression in C#";
            worksheet.Cells["B2"].Value = 5 + 5;

            // Add the formula
            worksheet.Cells["A3"].Value = "This is the formula";
            worksheet.Cells["B3"].Formula = "=RAND()";

            // Add external and internal links
            worksheet.Cells["A4"].Value = "These are hyperlinks";
            worksheet.Cells["B4"].Value = "External link";
            worksheet.Cells["B4"].Hyperlink = new ExcelHyperlink { Location = "https://sautinsoft.com" };
            worksheet.Cells["C4"].Value = "Internal link";
            worksheet.Cells["C4"].Hyperlink = new ExcelHyperlink { Location = "worksheet2!A1" };

            // Add the current time
            worksheet.Cells["A5"].Value = "This is DateTime";
            worksheet.Cells["B5"].Value = DateTime.Now;

            // Add a large composite text with formatting
            // Create a container of strings
            RichText text = new RichText();
            var part = new RichTextString("This is a very long string... ", new RichTextFormat { Italic = true, Bold = true, FontColor = SKColors.Blue });
            var part2 = new RichTextString("Which have several styles ",
                new RichTextFormat
                {
                    Italic = true,
                    Bold = true,
                    FontColor = SKColors.Green,
                    FontName = "Century",
                    FontSize = 20.2,
                });
            var part3 = new RichTextString("This is superscript text", new RichTextFormat { Strikethrough = true, Superscript = true, FontSize = 18 });
            var part4 = new RichTextString("This is subscript text", new RichTextFormat { Subscript = true, FontSize = 18 });
            
            // Add the following lines to the container
            text.Add(part);
            text.Add(part2);
            text.Add(part3);
            text.Add(part4);
            
            // Add the container to the cell
            worksheet.Cells["A6"].Value = text;

            // Print the properties of the document in a line and color it in a beautiful color
            worksheet.Cells["A8"].Value = $"This worksheet has name \"{worksheet.Name}\", uses {worksheet.Rows.Count} rows and {worksheet.CalculateMaxUsedColumns()} columns";
            worksheet.Cells["A8"].Style.Borders.SetBorders(MultipleBorders.Outside, SKColors.Cyan, LineStyle.Medium);
            worksheet.Cells["A8"].Style.Fill.SetSolid(SKColors.PaleTurquoise);

            // Add a string with numeric formatting
            worksheet.Cells["A9"].Value = .23451;
            worksheet.Cells["A9"].Style.NumberFormat = "#.##%";

            // Expand the columns to make them look attractive
            worksheet.Columns["A"].AutoFit();
            worksheet.Columns["B"].AutoFit();
            worksheet.Columns["C"].AutoFit();

            // Create a copy of the main page
            worksheet.InsertCopy("Just a copy worksheet", worksheet);

            // Save the document
            excelDocument.Save(outFile, new XlsSaveOptions());

            // Important for Linux: Install MS Fonts
            // sudo apt install ttf-mscorefonts-installer -y

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download

Option Infer On

Imports SautinSoft.Excel
Imports SkiaSharp
Imports System
Imports System.Text

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Get your free key here:   
			' https://sautinsoft.com/start-for-free/

			CreateExcelDocument()
		End Sub

		''' <summary>
		''' Creates a new XLS document.
		''' </summary>
		''' <remarks>
		''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/create-xls-document-net-csharp-vb.php
		''' </remarks>

		Private Shared Sub CreateExcelDocument()
			' Set a path to our Document
			Dim outFile As String = "..\..\..\Result.xls"

			' Create a new document
			Dim excelDocument As New ExcelDocument()

			' Add several worksheets
			excelDocument.Worksheets.Add("The main worksheet")
			excelDocument.Worksheets.Add("Second worksheet")

			' Create a variable to address
			Dim worksheet = excelDocument.Worksheets("The main worksheet")

			' Add plain text
			worksheet.Cells("A1").Value = "This is common string"
			worksheet.Cells("B1").Value = "Hello, World! 12345"

			' Add the result of  the expression
			worksheet.Cells("A2").Value = "This is the result of a mathematical expression in C#"
			worksheet.Cells("B2").Value = 5 + 5

			' Add the formula
			worksheet.Cells("A3").Value = "This is the formula"
			worksheet.Cells("B3").Formula = "=RAND()"

			' Add external and internal links
			worksheet.Cells("A4").Value = "These are hyperlinks"
			worksheet.Cells("B4").Value = "External link"
			worksheet.Cells("B4").Hyperlink = New ExcelHyperlink With {.Location = "https://sautinsoft.com"}
			worksheet.Cells("C4").Value = "Internal link"
			worksheet.Cells("C4").Hyperlink = New ExcelHyperlink With {.Location = "worksheet2!A1"}

			' Add the current time
			worksheet.Cells("A5").Value = "This is DateTime"
			worksheet.Cells("B5").Value = DateTime.Now

			' Add a large composite text with formatting
			' Create a container of strings
			Dim text As New RichText()
			Dim part = New RichTextString("This is a very long string... ", New RichTextFormat With {
				.Italic = True,
				.Bold = True,
				.FontColor = SKColors.Blue
			})
			Dim part2 = New RichTextString("Which have several styles ", New RichTextFormat With {
				.Italic = True,
				.Bold = True,
				.FontColor = SKColors.Green,
				.FontName = "Century",
				.FontSize = 20.2
			})
			Dim part3 = New RichTextString("This is superscript text", New RichTextFormat With {
				.Strikethrough = True,
				.Superscript = True,
				.FontSize = 18
			})
			Dim part4 = New RichTextString("This is subscript text", New RichTextFormat With {
				.Subscript = True,
				.FontSize = 18
			})

			' Add the following lines to the container
			text.Add(part)
			text.Add(part2)
			text.Add(part3)
			text.Add(part4)

			' Add the container to the cell
			worksheet.Cells("A6").Value = text

			' Print the properties of the document in a line and color it in a beautiful color
			worksheet.Cells("A8").Value = $"This worksheet has name ""{worksheet.Name}"", uses {worksheet.Rows.Count} rows and {worksheet.CalculateMaxUsedColumns()} columns"
			worksheet.Cells("A8").Style.Borders.SetBorders(MultipleBorders.Outside, SKColors.Cyan, LineStyle.Medium)
			worksheet.Cells("A8").Style.Fill.SetSolid(SKColors.PaleTurquoise)

			' Add a string with numeric formatting
			worksheet.Cells("A9").Value = .23451
			worksheet.Cells("A9").Style.NumberFormat = "#.##%"

			' Expand the columns to make them look attractive
			worksheet.Columns("A").AutoFit()
			worksheet.Columns("B").AutoFit()
			worksheet.Columns("C").AutoFit()

			' Create a copy of the main page
			worksheet.InsertCopy("Just a copy worksheet", worksheet)

			' Save the document
			excelDocument.Save(outFile, New XlsSaveOptions())

			' Important for Linux: Install MS Fonts
			' sudo apt install ttf-mscorefonts-installer -y

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу support@sautinsoft.ru или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.