.NET (1323)
Children categories
C#: Dynamically Create, Load, and Save Excel Files via Stream
2024-12-30 07:36:00 Written by daisy zhangUsing stream operations in C#, developers can dynamically create, load, and save Excel files, enabling flexible and efficient data handling. This approach eliminates the need for physical file storage, improving application performance and responsiveness. Ideal for real-time data manipulation or environments with storage limitations, it streamlines data exchange and system integration. This article demonstrates how to create, load, modify, and save Excel files using streams in C# with Spire.XLS for .NET, offering agile and scalable data management solutions.
- Dynamically Create an Excel File and Save It to Stream
- Load and Read Excel Files from Stream with C#
- Modify an Excel File in Stream with C#
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Dynamically Create an Excel File and Save It to Stream
Using Spire.XLS for .NET, developers can dynamically create Excel files in memory by initializing a Workbook object, populating it with data and formatting, and then saving the workbook to a stream using the Workbook.SaveToStream() method. This approach eliminates the need for physical file storage, enhancing both application performance and responsiveness.
Below are the steps for creating an Excel file and saving it to a stream with C#:
- Create an instance of the Workbook class to generate a new Excel workbook, which includes three default worksheets.
- Retrieve a specific worksheet using the Workbook.Worksheets[] property.
- Define the data to write to the worksheet, such as using a DataTable to organize the data.
- Insert the data into the worksheet using the Worksheet.InsertDataTable() method or the Worksheet.Range[].Value property for individual cell values.
- Format the worksheet cells, applying styles like colors, fonts, and borders, or adjusting column widths as needed.
- Save the workbook to a memory stream using the Workbook.SaveToStream() method. The stream can then be used for further processing, such as saving it to a file or transmitting it over a network.
- C#
using Spire.Xls;
using System.Data;
using System.Drawing;
namespace CreateExcelStream
{
class Program
{
static void Main(string[] args)
{
// Create a new workbook instance
Workbook workbook = new Workbook();
// Access the first worksheet in the workbook
Worksheet sheet = workbook.Worksheets[0];
// Create and populate a DataTable with sample data
DataTable dataTable = new DataTable("Data");
dataTable.Columns.Add("ID", typeof(int));
dataTable.Columns.Add("Name", typeof(string));
dataTable.Columns.Add("Age", typeof(int));
dataTable.Columns.Add("Country", typeof(string));
dataTable.Columns.Add("Salary ($)", typeof(decimal));
dataTable.Rows.Add(101, "John Smith", 28, "USA", 54000m);
dataTable.Rows.Add(102, "Maria Garcia", 34, "Spain", 65500m);
dataTable.Rows.Add(103, "Liam Johnson", 22, "Canada", 48000m);
dataTable.Rows.Add(104, "Emma Brown", 30, "Australia", 72300m);
dataTable.Rows.Add(105, "Wei Zhang", 40, "China", 58700m);
dataTable.Rows.Add(106, "Sofia Lopez", 26, "Mexico", 45200m);
// Insert data from the DataTable into the worksheet
sheet.InsertDataTable(dataTable, true, 1, 1);
// Format the worksheet
// Style the header row
sheet.Rows[0].Style.Color = Color.LightGreen;
sheet.Rows[0].Style.Font.FontName = "Arial";
sheet.Rows[0].Style.Font.Size = 12f;
sheet.Rows[0].BorderAround(); // Apply borders around the header row
sheet.Rows[0].Borders.Color = Color.Blue;
// Style the data rows
for (int i = 1; i < sheet.AllocatedRange.Rows.Count(); i++)
{
sheet.Rows[i].Style.Color = Color.LightGray;
sheet.Rows[i].Style.Font.FontName = "Arial";
sheet.Rows[i].Style.Font.Size = 11f;
}
// Adjust the column widths to fit the content
for (int j = 1; j <= sheet.AllocatedRange.Columns.Count(); j++)
{
sheet.AutoFitColumn(j);
}
// Save the workbook to a memory stream
MemoryStream stream = new MemoryStream();
workbook.SaveToStream(stream, FileFormat.Version2016);
// Write the stream content to a file
File.WriteAllBytes("output/CreateExcelByStream.xlsx", stream.ToArray());
// Release resources
workbook.Dispose();
}
}
}

Load and Read Excel Files from Stream with C#
Spire.XLS for .NET simplifies loading Excel files directly from a stream using the Workbook.LoadFromStream() method. Once the file is loaded, developers can easily access and read cell data, optimizing memory usage and enabling fast, flexible data processing without requiring file I/O operations.
The steps for loading and reading Excel files from streams with C# are as follows:
- Create a Workbook instance.
- Create a MemoryStream or FileStream object.
- Use the Workbook.LoadFromStream() method to load the Excel file from the stream into the workbook.
- Retrieve the first worksheet using the Workbook.Worksheets[] property.
- Loop through the rows and columns of the worksheet to extract the cell through the Worksheet.AllocatedRange[].Value property.
- Print the extracted data, or use the data for further operations.
- C#
using Spire.Xls;
namespace LoadExcelStream
{
class Program
{
static void Main(string[] args)
{
// Create an instance of the Workbook class
Workbook workbook = new Workbook();
// Create a memory stream
MemoryStream stream = new MemoryStream();
File.OpenRead("Sample.xlsx").CopyTo(stream);
// Load the Excel file from the stream
workbook.LoadFromStream(stream);
// Access the first worksheet in the workbook
Worksheet sheet = workbook.Worksheets[0];
// Initialize a list to store the data retrieved from the worksheet
List<List<string>> data = new List<List<string>>();
for (int i = 0; i < sheet.AllocatedRange.Rows.Count(); i++)
{
// Create a list to hold each row of data
List<string> lines = new List<string>();
for (int j = 0; j < sheet.AllocatedRange.Columns.Count(); j++)
{
// Retrieve the cell text and add it to the row
lines.Add(sheet.AllocatedRange[i + 1, j + 1].Text);
}
// Add the row to the data list
data.Add(lines);
}
// Print the retrieved data or use it for further operations
foreach (List<string> lines in data)
{
Console.WriteLine(string.Join(" | ", lines));
}
}
}
}

Modify an Excel File in Stream with C#
With Spire.XLS for .NET, developers can modify an Excel file in memory by first loading it into a Workbook object with the LoadFromStream() method. After making updates (such as changing cell values or formatting), the file can be saved back to a stream using the Workbook.SaveToStream() method. This approach allows seamless real-time changes without relying on physical storage.
Follow the steps below to modify Excel files in streams with C#:
- Create a Workbook instance to represent the Excel file.
- Create a MemoryStream or FileStream instance.
- Use the Workbook.LoadFromStream() to load the Excel file from the stream.
- Access the first worksheet through the Workbook.Worksheets[] property.
- Modify the header row and the data rows' styles (font, size, background color, etc.) through the properties in CellRange.Style.
- Autofit the columns to adjust their width based on the content using the Worksheet.AutoFitColumn() method.
- Save the changes to the stream using the Workbook.SaveToStream() method.
- C#
using Spire.Xls;
using System.Drawing;
namespace ModifyExcelStream
{
class Program
{
static void Main(string[] args)
{
// Create a new instance of the Workbook class
Workbook workbook = new Workbook();
// Create a memory stream
MemoryStream stream = new MemoryStream();
File.OpenRead("Sample.xlsx").CopyTo(stream);
// Load the Excel file from the stream
workbook.LoadFromStream(stream);
// Access the first worksheet in the workbook
Worksheet sheet = workbook.Worksheets[0];
// Modify the style of the header row
CellRange headerRow = sheet.AllocatedRange.Rows[0];
headerRow.Style.Font.FontName = "Times New Roman";
headerRow.Style.Font.Size = 12f;
headerRow.Style.Color = Color.LightBlue;
// Modify the style of the data rows
for (int i = 1; i < sheet.AllocatedRange.Rows.Count(); i++)
{
CellRange dataRow = sheet.AllocatedRange.Rows[i];
dataRow.Style.Font.FontName = "Arial";
dataRow.Style.Font.Size = 10f;
dataRow.Style.Color = Color.LightGray;
// Alternate row coloring (even rows)
if (i % 2 == 0)
{
dataRow.Style.Color = Color.LightSlateGray;
}
}
// Autofit columns to adjust their width based on content
for (int k = 1; k <= sheet.AllocatedRange.Columns.Count(); k++)
{
sheet.AutoFitColumn(k);
}
// Change the border color
sheet.AllocatedRange.Style.Borders.Color = Color.White;
// Save the modified workbook back to the stream
workbook.SaveToStream(stream);
// Write the stream content to a new file
File.WriteAllBytes("output/ModifyExcelByStream.xlsx", stream.ToArray());
// Release resources
workbook.Dispose();
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
XPS (XML Paper Specification) is a fixed-layout document format designed to preserve document fidelity and provide device-independent document appearance. It is similar to PDF, but is based on XML rather than PostScript. If you want to save a Word document to a fixed-layout file format, XPS would be an option. This article will demonstrate how to convert Word documents to XPS in C# and VB.NET using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Convert Word to XPS in C# and VB.NET
The following are the detailed steps to convert a Word document to XPS using Spire.Doc for .NET:
- Initialize an instance of Document class.
- Load a Word document using Document.LoadFromFile() method.
- Save the Word document to XPS using Document.SaveToFile(string filePath, FileFormat fileFormat) method.
- C#
- VB.NET
using Spire.Doc;
namespace ConvertWordToXps
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("Sample.docx");
//convert the document to XPS
doc.SaveToFile("ToXPS.xps", FileFormat.XPS);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
XPS is short for XML Paper Specification developed by Microsoft, which is a specification for a page description language and a fixed-document format. It comes out by Microsoft’s initiative to associate file creation with reading in its Windows operating system. Like PDF, XPS plays a loyal role to preserve document with offering device-independent document appearance. Editing in XPS or in PDF seems difficult.
As a flexible and professional component, Spire.PDF for .NEToffers a large variety conversion, among which the conversion from XPS to PDF is one of its popular feature. In addition, Spire.PDF for .NET can be applied in WinForm, ASP.NET and Console Application.
The following code example shows how to convert XPS files to PDF document.
Step 1: Introduce a class named pdfDocument which is used to initialize a Spire.PDF.PdfDocument, and load a XPS file by calling the method of LoadForm File.
PdfDocument doc = new PdfDocument(); doc.LoadFromFile(xpsFile,FileFormat.XPS);
Step 2: Only needs one row of simple code. Call the SavetoFile method of Spire.PDF.pdfDocument to save all the data as PDF formart.
doc.SaveToFile(pdfFile, FileFormat.PDF);
After this code, run this application and you will see the PDF converted from XPS.
Screenshot before converting XPS to PDF:

Screenshot after converting XPS to PDF:

Programme Guide for Spire.Barcode
Spire.BarCode for .NET is a professional barcode component specially designed for .NET developers (C#, VB.NET, ASP.NET) to generate, read 1D & 2D barcodes. Developers and programmers can use Spire.BarCode to add Enterprise-Level barcode formats to their .net applications (ASP.NET, WinForms) quickly and easily. Below is how to use Spire.BarCode for .NET.
Step 1: Create Project
Create a C#/VB.NET Windows Forms Application project in visual studio, name it Barcode.
Step 2: Add Spire.Barcode.dll
- In Toolbox, right-click the blank area, select "Add Tab", name it "Spire.Barcode Controls".
- Right-click "Spire.Barcode Controls", select "Choose Items", select ".NET Framework Components", Click "Browse", find the Spire.Barcode.dll and double-click it.
- Then you will see "BarCodeControl" shown in "Spire.Barcode Controls" in Toolbox.
Step 3: Add Controls
Here is what the window looks like:

Step 4: Generate Barcode Image
btnCreate_Click is the method to generate barcode image. There are two import classes-BarCodeControl and BarCodeGenerator in this method. BarCodeControl stores information about barcode.
Here are some introductions about some of its properties:
| Name of Property | Description |
| Data | Stores the data that is to be encoded to one-dimension barcode. |
| Data2D | Stores the data that is to be encoded to two-dimension barcode. |
| Type | Indicates the type of barcode that is to be generated. |
| HasBorder | Indicates whether barcode image has border. |
| BorderDashStyle | Stores the type of border barcode image has. |
| BarHeight | Stores the height of barcode image. |
| CheckB_BarcodeText | Indicates whether to show the barcode text. |
| TextFont | Stores the font of barcode text. |
| ForeColor | Stores the fore color of barcode image. |
| CheckB_Sum | Indicates whether to show the checksum digit in Code128 and EAN128 Barcodes. |
BarCodeGenerator is the class to generate barcode image. Its constructor takes one parameter – a BarCodeControl instance. It has a method called GenerateImage() whose return value is Image object to generate image.
//Generate the barcode based on the this.barCodeControl1
BarCodeGenerator generator = new BarCodeGenerator(this.barCodeControl1);
Image barcode = generator.GenerateImage();
//save the barcode as an image
barcode.Save("barcode.png");
'Generate the barcode based on the barCodeControl1
Dim generator As New BarCodeGenerator(barCodeControl1)
Dim barcode As Image = generator.GenerateImage()
'save the barcode as an image
barcode.Save("barcode.png")
Step 5: Scan Barcode Image
BarcodeScanner is the class to scan barcode image. Call its method Scan with the Bitmap object containing the barcode image, it returns a string [] value where the scanning result is stored.
And btnScan_Click uses the class BarcodeScanner to scan barcode image in its code.
Here is the code in btnScan_Click.
private void btnScan_Click(object sender, EventArgs e)
{
//scan the barcode
string[] datas = Spire.Barcode.BarcodeScanner.Scan("barcode.png");
//show the scan result
this.TextB_ScanResult.Text = datas[0];
}
Private Sub btnScan_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnScan.Click
'scan the barcode
Dim datas() As String = Spire.Barcode.BarcodeScanner.Scan("barcode.png")
'show the scan result
Me.TextB_ScanResult.Text = datas(0)
End Sub
Step 6. Running
Run the project. You will see a window opened.

Put in some settings and click the button "Create". You will see the generated barcode image.

Click the button "Scan". You will see the barcode text shown in TextB_ScanResult.

As we know, when we add a comment in MS-Excel, there will be Author appended automatically by Excel program. In fact, there is not the Author property in the comment in MS-Excel, which is just a text with special bold font style, and you can edit, remove, and insert other strings. After adding a comment (e.g., Test comment1) by using Spire.XLS component, there is no author is written into excel file. Check the below picture:

How to add a comment with editable Author property by Spire.XLS component? Just like the comment in the following picture:

Download Spire.XLS for .NET and install it on your computer. Then create CreateComment(CellRange range,string text, string author=null) method to implement it. Firstly, add a comment for a specified range by calling the CellRange.AddComment() method. Then, if the author is specified, create a new font with bold style and apply the font to the author text.
The following is the code for the method:
static ExcelComment CreateComment(CellRange range,string text, string author=null)
{
ExcelComment comment= range.AddComment();
comment.Text = String.IsNullOrEmpty(author) ? text : author + ":\n" + text;
if (!String.IsNullOrEmpty(author))
{
ExcelFont font = range.Worksheet.Workbook.CreateFont();
font.FontName = "Tahoma";
font.KnownColor = ExcelColors.Black;
font.IsBold = true;
comment.RichText.SetFont(0, author.Length,font);
}
return comment;
}
Private Function CreateComment(ByVal range As CellRange, ByVal text As String, Optional ByVal author As String = Nothing) As ExcelComment
Dim comment As ExcelComment = range.AddComment()
comment.Text = If([String].IsNullOrEmpty(author), text, author & ":" & vbLf & text)
If Not [String].IsNullOrEmpty(author) Then
Dim font As ExcelFont = range.Worksheet.Workbook.CreateFont()
font.FontName = "Tahoma"
font.KnownColor = ExcelColors.Black
font.IsBold = True
comment.RichText.SetFont(0, author.Length, font)
End If
Return comment
End Function
Next, you can call the method to add comments with author to your excel file.
CellRange range = sheet.Range["A1"]; CreateComment(range, "Test comment2", "E-iceblue");
Dim range As CellRange = sheet.Range("A1")
CreateComment(range, "Test comment2", "E-iceblue")
Tables in Word documents allow users to organize data in a structured, readable format. However, at times you may find that some tables are outdated or no longer serve their intended purpose, making it necessary to remove them. In this article, you will learn how to remove tables from a Word document in C# using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Remove a Specified Table in Word in C#
Spire.Doc for .NET provides the Section.Tables.RemoveAt(int index) method to delete a specified table in a Word document by index. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Get a specified section using Document.Sections[] property.
- Delete a specified table by index using Section.Tables.RemoveAt() method.
- Save the result document using Document.SaveToFile() method.
- C#
using Spire.Doc;
namespace RemoveTable
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("tables.docx");
//Get the first section in the document
Section sec = doc.Sections[0];
//Remove the first table in the section
sec.Tables.RemoveAt(0);
//Save the result document
doc.SaveToFile("RemoveATable.docx", FileFormat.Docx);
}
}
}

Remove All Tables in Word in C#
To delete all tables from a Word document, you need to iterate through all sections in the document, then iterate through all tables in each section and remove them through the Section.Tables.Remove() method. The following are the detailed steps.
- Create a Document instance.
- Load a Word document using Document.LoadFromFile() method.
- Iterate through all sections in the document.
- Iterate through all tables in each section.
- Delete the tables using Section.Tables.Remove() method.
- Save the result document using Document.SaveToFile() method.
- C#
using Spire.Doc;
namespace RemoveAllTable
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();
//Load a Word document
doc.LoadFromFile("tables.docx");
//Iterate through all sections in the document
foreach (Section section in doc.Sections)
{
//Iterate through all tables in each section
foreach (Table table in section.Tables)
{
//Remove the tables
section.Tables.Remove(table);
}
}
//Save the result document
doc.SaveToFile("RemoveTables.docx", FileFormat.Docx);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
When the data in specific rows or columns are no longer needed, you can delete those rows or columns from your worksheet. In this article, you will learn how to delete rows and columns from Excel in C# and VB.NET using Spire.XLS for .NET library.
Install Spire.XLS for.NET
To begin with, you need to add the DLL files included in the Spire.XLS for.NET package as references in your .NET project. The DLLs files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Delete a Specific Row and Column from Excel in C# and VB.NET
The following are the steps to delete a specific row and column from an Excel worksheet:
- Create a Workbook instance.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Delete the desired row from the worksheet by its index (1-based) using Worksheet.DeleteRow(rowIndex) method.
- Delete the desired column from the worksheet by its index (1-based) using Worksheet.DeleteColumn(columnIndex) method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace DeleteRowAndColumn
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("Sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Delete the 9th row
sheet.DeleteRow(9);
//Delete the 3rd column
sheet.DeleteColumn(3);
//Save the result file
workbook.SaveToFile("DeleteRowAndColumn.xlsx", ExcelVersion.Version2016);
}
}
}

Delete Multiple Rows and Columns from Excel in C# and VB.NET
The following are the steps to delete multiple rows and columns from an Excel worksheet:
- Create a Workbook instance.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get the desired worksheet using Workbook.Worksheets[sheetIndex] property.
- Delete the desired rows from the worksheet using Worksheet.DeleteRow(startRowIndex, rowCount) method.
- Delete the desired columns from the worksheet using Worksheet.DeleteColumn(startColumnIndex, columnCount) method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace DeleteMultipleRowsAndColumns
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile(@"Sample.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Delete 3 rows from the worksheet starting from the 7th row
sheet.DeleteRow(7, 3);
//Delete 3 columns from the worksheet starting from the 3rd column
sheet.DeleteColumn(3, 3);
//Save the result file
workbook.SaveToFile("DeleteMultipleRowsAndColumns.xlsx", ExcelVersion.Version2016);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
When dealing with Excel files containing large amounts of data, you may sometimes need to hide certain rows and columns to conceal useless data so that you can focus on the information you need to analyze. In this article, you will learn how to hide or show rows and columns in Excel in C# and VB.NET using Spire.XLS for .NET.
- Hide Specific Rows and Columns in Excel
- Show Specific Hidden Rows and Columns in Excel
- Hide Multiple Rows and Columns at Once in Excel
- Show All Hidden Rows and Columns in Excel
Install Spire.XLS for .NET
To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.XLS
Hide Specific Rows and Columns in Excel in C# and VB.NET
The following steps demonstrate how to hide specific rows and columns in Excel in C# and VB.NET:
- Initialize an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[int sheetIndex] property.
- Hide specific rows in the worksheet using Worksheet.HideRow(int rowIndex) method.
- Hide Specific columns in the worksheet using Worksheet.HideColumn(int columnIndex) method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace HideExcelRowsAndColumns
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("Input.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Hide the 3rd and the 7th rows
sheet.HideRow(3);
sheet.HideRow(7);
//Hide the 3rd and the 6th columns
sheet.HideColumn(3);
sheet.HideColumn(6);
//Save the result file
workbook.SaveToFile("HideRowsAndColumns.xlsx", ExcelVersion.Version2013);
}
}
}

Show Specific Hidden Rows and Columns in Excel in C# and VB.NET
The following steps demonstrate how to show specific hidden rows and columns in Excel in C# and VB.NET:
- Initialize an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[int sheetIndex] property.
- Show specific hidden rows in the worksheet using Worksheet.ShowRow(int rowIndex) method.
- Show specific hidden columns in the worksheet using Worksheet.ShowColumn(int columnIndex) method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace ShowExcelRowsAndColumns
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("HideRowsAndColumns.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Show the 3rd and the 7th rows
sheet.ShowRow(3);
sheet.ShowRow(7);
//Show the 3rd and the 6th columns
sheet.ShowColumn(3);
sheet.ShowColumn(6);
//Save the result file
workbook.SaveToFile("ShowRowsAndColumns.xlsx", ExcelVersion.Version2013);
}
}
}

Hide Multiple Rows and Columns at Once in Excel in C# and VB.NET
The following steps demonstrate how to hide multiple rows and columns at once in Excel in C# and VB.NET:
- Initialize an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[int sheetIndex] property.
- Hide multiple rows in the worksheet using Worksheet.HideRows(int rowIndex, int rowCount) method.
- Hide multiple columns in the worksheet using Worksheet.HideColumns(int columnIndex, int columnCount) method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace HideMultipleExcelRowsAndColumns
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("Input.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Hide 3, 4 and 5 rows
sheet.HideRows(3, 3);
//Hide 5, 6 and 7 columns
sheet.HideColumns(5, 3);
//Save the result file
workbook.SaveToFile("HideMultipleRowsAndColumns.xlsx", ExcelVersion.Version2013);
}
}
}

Show All Hidden Rows and Columns in Excel in C# and VB.NET
The following steps demonstrate how to show all hidden rows and columns in Excel in C# and VB.NET:
- Initialize an instance of the Workbook class.
- Load an Excel file using Workbook.LoadFromFile() method.
- Get a specific worksheet through Workbook.Worksheets[int sheetIndex] property.
- Iterate through the rows in the worksheet and find the hidden rows using Worksheet.GetRowIsHide(int rowIndex) method.
- Show all hidden rows using Worksheet.ShowRow(int rowIndex) method.
- Iterate through the columns in the worksheet and find the hidden columns using Worksheet.GetColumnIsHide(int columnIndex) method.
- Show all hidden columns using Worksheet.ShowColumn(int columnIndex) method.
- Save the result file using Workbook.SaveToFile() method.
- C#
- VB.NET
using Spire.Xls;
namespace ShowAllHiddenRowsAndColumns
{
class Program
{
static void Main(string[] args)
{
//Create a Workbook instance
Workbook workbook = new Workbook();
//Load an Excel file
workbook.LoadFromFile("HideRowsAndColumns.xlsx");
//Get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//Iterate through the rows in the worksheet
for (int i = 1; i <= sheet.LastRow; i++)
{
//Check if the current row is hidden
if (sheet.GetRowIsHide(i))
{
//Show the hidden row
sheet.ShowRow(i);
}
}
//Iterate through the columns in the worksheet
for (int j = 1; j <= sheet.LastRow; j++)
{
//Check if the current column is hidden
if (sheet.GetColumnIsHide(j))
{
//Show the hidden column
sheet.ShowColumn(j);
}
}
//Save the result file
workbook.SaveToFile("ShowAllHiddenRowsAndColumns.xlsx", ExcelVersion.Version2013);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
EPUB is a standard file format for publishing eBooks or other electronic documents. The content in an EPUB file is reflowable, which means that the content automatically adjusts itself to fit the screen it is being displayed on. People who want to publish their eBooks may need to convert their works stored in Word documents to EPUB files. In this article, you will learn how to programmatically achieve this task using Spire.Doc for .NET.
Install Spire.Doc for .NET
To begin with, you need to add the DLL files included in the Spire.Doc for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.Doc
Convert Word to EPUB
The detailed steps are as follows:
- Create a Document instance.
- Load a sample Word document using Document.LoadFromFile() method.
- Save the document to EPUB using Document.SaveToFile() method.
- C#
- VB.NET
using Spire.Doc;
namespace WordtoEPUB
{
class Epub
{
static void Main(string[] args)
{
//Create a Document instance
Document document = new Document();
//Load a sample Word document
document.LoadFromFile("demo.docx");
//Convert the Word document to EPUB
document.SaveToFile("ToEpub.epub", FileFormat.EPub);
}
}
}

Convert Word to EPUB with a Cover Image
The detailed steps are as follows.
- Create a Document instance.
- Load a sample Word document using Document.LoadFromFile() method.
- Create a DocPicture instance, and then load an image using DocPicture.LoadImage() method.
- Save the Word document to EPUB with cover image using Document.SaveToEpub(String, DocPicture) method.
- C#
- VB.NET
using Spire.Doc;
using Spire.Doc.Fields;
using System.Drawing;
namespace ConvertWordToEpubWithCoverImage
{
class Program
{
static void Main(string[] args)
{
//Create a Document instance
Document doc = new Document();
//Load a sample Word document
doc.LoadFromFile("demo.docx");
//Create a DocPicture instance
DocPicture picture = new DocPicture(doc);
//Load an image
picture.LoadImage(Image.FromFile("CoverImage.png"));
//Save the Word document to EPUB with cover image
doc.SaveToEpub("ToEpubWithCoverImage.epub", picture);
}
}
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
This section aims at providing developers a solution on how to insert Excel background image in C#, VB.NET via a .NET Excel library Spire.XLS for .NET.
Spire.XLS for .NET enables you to add background image in your Excel file by setting BackgroundImage property of the object PageSetup to be the image you want to insert, you can see the code here: Spire.Xls.Worksheet.PageSetup.BackgroundImage. Now let us start our task.
Task:
If you are not familiar with Spire.XLS for .NET, please allow me to give a short description of it. Spire.XLS for .NET is an Excel application which enables users to perform a wide range of processing tasks in Excel documents such as generate, edit and handle Excel files in C#.
You will use Spire.XLS for .NET, so we can download Spire.XLS for .NET first. After installing it on system, please start your project either in C# Console Application or VB.NET Console Application. You also can create your project in Windows Forms Application. Please do not forget to add Spire.Xls as reference. The default path is “..\Spire.XLS\Bin\Bin\NET4.0\ Spire.XLS.dll”. Below shows the whole code of my project:
using Spire.Xls;
using System.Drawing;
namespace InsertExcelBackgroundImage
{
class Program
{
static void Main(string[] args)
{
//initialize a new instance of Workbook
Workbook workbook = new Workbook();
//import an Excel file from system
workbook.LoadFromFile(@"..\Excel background image.xlsx");
//get the first worksheet
Worksheet sheet = workbook.Worksheets[0];
//open an image
Bitmap bm = new Bitmap(Image.FromFile(@"..\butterflytable.jpg"));
//set the image to be background image of the worksheet
sheet.PageSetup.BackgoundImage = bm;
//save excel file
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
//launch the file
System.Diagnostics.Process.Start("result.xlsx");
}
}
}
Imports Spire.Xls
Imports System.Drawing
Namespace InsertExcelBackgroundImage
Class Program
Private Shared Sub Main(args As String())
'initialize a new instance of Workbook
Dim workbook As New Workbook()
'import an Excel file from system
workbook.LoadFromFile("..\Excel background image.xlsx")
'get the first worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'open an image
Dim bm As New Bitmap(Image.FromFile("..\butterflytable.jpg"))
'set the image to be background image of the worksheet
sheet.PageSetup.BackgoundImage = bm
'save excel file
workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
'launch the file
System.Diagnostics.Process.Start("result.xlsx")
End Sub
End Class
End Namespace
Output File:
After executing above code, I successfully add background image in Excel file, you can see the output file as below:

In this section, I have introduced the detail solution on how to insert background image in Excel file with C#, VB.NET. I wish it can help you. If you have any questions, comments or advice, you can add it at E-iceblue Forum, our professionals will give prompt reply.