How to Use C#/VB.NET to Add Excel Borders
How to Use C#/VB.NET to Add Excel Borders
Why We Add Excel Borders?
How to Add Excel Borders in Microsoft Excel?
How to Use Spire.XLS for .NET to Add Excel Borders?
using System.Drawing;
using Spire.Xls;
namespace ExtractText
{
class Program
{
static void Main(string[] args)
{
//Create a new workbook.
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//Write text in B2.
sheet.Range["B2"].Text = "Home page";
//Set the color of the six borders.
sheet.Range["B2"].Borders.Color = Color.Green;
//Set the line style of the borders.
sheet.Range["B2"].Borders.LineStyle = LineStyleType.Thick;
//Delete the diagonalDown border.
sheet.Range["B2"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;
//Delete the DiagonalUp border.
sheet.Range["B2"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;
sheet.AutoFitColumn(2);
//Save the file.
workbook.SaveToFile("Sample.xls");
//Launch the file.
System.Diagnostics.Process.Start("Sample.xls");
}
}
}
Imports Microsoft.VisualBasic
Imports System.Drawing
Imports Spire.Xls
Module Module1
Sub Main()
'Create a new workbook.
Dim workbook As Workbook = New Workbook()
Dim sheet As Worksheet = workbook.Worksheets(0)
'Write text in B2.
sheet.Range("B2").Text = "Home page"
'Set the color of the six borders.
sheet.Range("B2").Borders.Color = Color.Green
'Set the line style of the borders.
sheet.Range("B2").Borders.LineStyle = LineStyleType.Thick
'Delete the diagonalDown border.
sheet.Range("B2").Borders(BordersLineType.DiagonalDown).LineStyle = LineStyleType.None
'Delete the DiagonalUp border.
sheet.Range("B2").Borders(BordersLineType.DiagonalUp).LineStyle = LineStyleType.None
sheet.AutoFitColumn(2)
'Save the file.
workbook.SaveToFile("Sample.xls")
'Launch the file.
System.Diagnostics.Process.Start("Sample.xls")
End Sub
End Module

XLS to PDF for C#, VB.NET
The sample demonstrates how to convert Excel workbook to PDF file via Spire.XLS.

How to Export Datatable to Excel through DataGridView
How to Export DataTable to Excel through DataGridView
Export DataTable to Excel can be very easy through Spire.DataExport (or Spire.Office). Furthermore, Spire.DataExport enables user to Export data to Excel through DataGridView. Through DataGridView users can preview and modify data information before exporting.
Download Spire.DataExport (or Spire.Office) with .NET Framework together. Only 2 Simple steps you can finish the whole datatable to Excel exporting process.
Step 1, Load Data Information
In this step, Spire.DataExport will help you load Data information from your datatable. After you put in your data source and SQL command, you can preview and modify data information in DataGridView area.
private void btnLoad_Click(object sender, EventArgs e)
{
using(OleDbConnection oleDbConnection = new OleDbConnection())
{
oleDbConnection.ConnectionString = this.textBox1.Text;
OleDbCommand oleDbCommand = new OleDbCommand();
oleDbCommand.CommandText = this.textBox2.Text;
oleDbCommand.Connection = oleDbConnection;
using(OleDbDataAdapter da = new OleDbDataAdapter(oleDbCommand))
{
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
}
}
Effect Screenshot

Step 2, Set Export into Excel
Spire.DataExport allows user to export data into most popular file formats including MS Excel, MS Word, HTML, PDF, XML, CSV, DBF, DIF, etc. Now, in this step you should give an order to Export Data into Excel file format. Spire.DataExport will create a new MS Excel Worksheet for storing Data which exported out. You can rename the excel file in this step either.
private void btnRun_Click(object sender, EventArgs e)
{
Spire.DataExport.XLS.CellExport cellExport = new Spire.DataExport.XLS.CellExport();
Spire.DataExport.XLS.WorkSheet worksheet1 = new Spire.DataExport.XLS.WorkSheet();
worksheet1.DataSource = Spire.DataExport.Common.ExportSource.DataTable;
worksheet1.DataTable = this.dataGridView1.DataSource as DataTable;
worksheet1.StartDataCol = ((System.Byte)(0));
cellExport.Sheets.Add(worksheet1);
cellExport.ActionAfterExport = Spire.DataExport.Common.ActionType.OpenView;
cellExport.SaveToFile("20110223.xls");
}
Effect Screenshot


Save Excel Document with C# and VB.NET
Workbook Save in MS
C# excel workbook.saveas via Spire.XLS
using Spire.Xls;
namespace Saveas
{
class Program
{
static void Main(string[] args)
{
//Create a new workbook
Workbook workbook = new Workbook();
//Initialize worksheet
Worksheet sheet = workbook.Worksheets[0];
//Append text
sheet.Range["A1"].Text = "demo";
//Save it as XML file
workbook.SaveAsXml("Sample.xml");
//Launch the file
System.Diagnostics.Process.Start("Sample.xml");
}
}
}
Imports Spire.Xls
Module Module1
Sub Main()
'Create a new workbook
Dim workbook As New Workbook()
'Initialize worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'Append text
sheet.Range("A1").Text = "demo"
'Save it as XML file.
workbook.SaveAsXml("Sample.xml")
'Launching the MS Word file.
System.Diagnostics.Process.Start("Sample.xml")
End Sub
End Module
How to Add a Formula for Excel
Introduction
How to Add Formula for Excel in MS
How to Add Formula for Excel with Spire.XLS
using Spire.Xls;
namespace Formula
{
class Program
{
static void Main(string[] args)
{
//Create a new workbook
Workbook workbook = new Workbook();
//Initialize worksheet
Worksheet sheet = workbook.Worksheets[0];
//initialize currentRow
int currentRow = 3;
string currentFormula = string.Empty;
//test data
sheet.Range[currentRow, 2].NumberValue = 7.3;
sheet.Range[currentRow, 3].NumberValue = 5; ;
sheet.Range[currentRow, 4].NumberValue = 8.2;
sheet.Range[currentRow, 5].NumberValue = 4;
sheet.Range[currentRow, 6].NumberValue = 3;
sheet.Range[currentRow, 7].NumberValue = 11.3;
//string.
currentFormula = "=\"hello\"";
sheet.Range[++currentRow, 1].Text = "=\"hello\"";
sheet.Range[currentRow, 2].Formula = currentFormula;
//bool.
currentFormula = "=false";
sheet.Range[++currentRow, 1].Text = currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//calculation
currentFormula = "=33*3/4-2+10";
sheet.Range[++currentRow, 1].Text = currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//sheet area reference
currentFormula = "=AVERAGE(Sheet1!$D$3:G$3)";
sheet.Range[++currentRow, 1].Text = currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
//time
currentFormula = "=NOW()";
sheet.Range[++currentRow, 1].Text = currentFormula;
sheet.Range[currentRow, 2].Formula = currentFormula;
sheet.Range[currentRow, 2].Style.NumberFormat = "yyyy-MM-DD";
//Save the file
workbook.SaveToFile("Sample.xls");
//Launch the file
System.Diagnostics.Process.Start("Sample.xls");
}
}
}
Imports Spire.Xls
Module Module1
Sub Main()
'Create a new workbook
Dim workbook As New Workbook()
'Initialize worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'initialize currentRow
Dim currentRow As Integer = 3
Dim currentFormula As String = String.Empty
'test data
sheet.Range(currentRow, 2).NumberValue = 7.3
sheet.Range(currentRow, 3).NumberValue = 5
sheet.Range(currentRow, 4).NumberValue = 8.2
sheet.Range(currentRow, 5).NumberValue = 4
sheet.Range(currentRow, 6).NumberValue = 3
sheet.Range(currentRow, 7).NumberValue = 11.3
'string.
currentFormula = "=""hello"""
sheet.Range(System.Threading.Interlocked.Increment(currentRow), 1).Text = "=""hello"""
sheet.Range(currentRow, 2).Formula = currentFormula
'bool.
currentFormula = "=false"
sheet.Range(System.Threading.Interlocked.Increment(currentRow), 1).Text = currentFormula
sheet.Range(currentRow, 2).Formula = currentFormula
'calculation
currentFormula = "=33*3/4-2+10"
sheet.Range(System.Threading.Interlocked.Increment(currentRow), 1).Text = currentFormula
sheet.Range(currentRow, 2).Formula = currentFormula
'sheet area reference
currentFormula = "=AVERAGE(Sheet1!$D$3:G$3)"
sheet.Range(System.Threading.Interlocked.Increment(currentRow), 1).Text = currentFormula
sheet.Range(currentRow, 2).Formula = currentFormula
'time
currentFormula = "=NOW()"
sheet.Range(System.Threading.Interlocked.Increment(currentRow), 1).Text = currentFormula
sheet.Range(currentRow, 2).Formula = currentFormula
sheet.Range(currentRow, 2).Style.NumberFormat = "yyyy-MM-DD"
'Save doc file.
workbook.SaveToFile("Sample.xls")
'Launching the MS Word file.
System.Diagnostics.Process.Start("Sample.xls")
End Sub
End Module

How to Insert an Image into an Excel Spreadsheet
What is Excel Image?
How to Insert an Image in Excel with Spire.XLS?
using Spire.Xls;
namespace Image
{
class Program
{
static void Main(string[] args)
{
//Create a new workbook
Workbook workbook = new Workbook();
//Initialize the worksheet
Worksheet sheet = workbook.Worksheets[0];
//Insert an image
sheet.Pictures.Add(1, 1, @"..\..\..\..\..\..\Data\day.jpg");
//Save the file
workbook.SaveToFile("sample.xls");
//Launch the file
System.Diagnostics.Process.Start("Sample.xls");
}
}
}
Imports Spire.Xls
Module Module1
Sub Main()
'Create a new workbook
Dim workbook As New Workbook()
'Initialize worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'Insert an image
sheet.Pictures.Add(1, 1, "..\..\..\..\..\..\Data\day.jpg")
'Save doc file.
workbook.SaveToFile("Sample.xls")
'Launching the MS Word file.
System.Diagnostics.Process.Start("Sample.xls")
End Sub
End Module
How to Create Excel Pie Charts for C#/VB.NET
Why We Create Excel Pie Charts?
How to Create Excel Pie Charts in Microsoft Excel?
- Launch Microsoft Office Excel and open your excel file which with the data that you want to base your pie chart on.
- Select the data that you want to base your chart on. The last cell that you want to select will not be chosen but it will have bold outline borders around it.
- Click "Insert" button to open Chart Wizard window. Click on "Pie" in the right side column of Chart Type. Several sub-types of pie charts will be offered for you to choose. Pick a proper type which can best match with your data and click "Next"
- Prevew on this new interface. If the pie chart with wrong information or even no pie chart appears, click "Cancel" and select your data, try the above process again. If everything is OK, click "Next"
- On the new window, enter labels and a title for your pie chart. Click "Finish" when you see the pie chart appears as you want.
- On your spreadsheet, click the box and drag your mouse to manipulate the size of the chart. By right clicking the chart and box, you can get many options to edit the pie chart.
How to Use Spire.XLS for .NET to Creat Excel Pie Charts?
using Spire.Xls;
using System.Drawing;
namespace Saveas
{
class Program
{
static void Main(string[] args)
{
//Create a new workbook
Workbook workbook = new Workbook();
//Initialize worksheet
workbook.CreateEmptySheets(1);
Worksheet sheet = workbook.Worksheets[0];
//Set sheet name
sheet.Name = "Chart data";
//Set the grid lines invisible
sheet.GridLinesVisible = false;
//Create a chart
Chart chart = sheet.Charts.Add(ExcelChartType.Pie3D);
//Set region of chart data
chart.DataRange = sheet.Range["B2:B5"];
chart.SeriesDataFromRange = false;
//Set position of chart
chart.LeftColumn = 1;
chart.TopRow = 6;
chart.RightColumn = 9;
chart.BottomRow = 25;
//Chart title
chart.ChartTitle = "Sales by year";
chart.ChartTitleArea.IsBold = true;
chart.ChartTitleArea.Size = 12;
//Initialize the chart series
Spire.Xls.Charts.ChartSerie cs = chart.Series[0];
//Chart Labels resource
cs.CategoryLabels = sheet.Range["A2:A5"];
//Chart value resource
cs.Values = sheet.Range["B2:B5"];
//Set the value visible in the chart
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = true;
//Year
sheet.Range["A1"].Value = "Year";
sheet.Range["A2"].Value = "2002";
sheet.Range["A3"].Value = "2003";
sheet.Range["A4"].Value = "2004";
sheet.Range["A5"].Value = "2005";
//Sales
sheet.Range["B1"].Value = "Sales";
sheet.Range["B2"].NumberValue = 4000;
sheet.Range["B3"].NumberValue = 6000;
sheet.Range["B4"].NumberValue = 7000;
sheet.Range["B5"].NumberValue = 8500;
//Style
sheet.Range["A1:B1"].Style.Font.IsBold = true;
sheet.Range["A2:B2"].Style.KnownColor = ExcelColors.LightYellow;
sheet.Range["A3:B3"].Style.KnownColor = ExcelColors.LightGreen1;
sheet.Range["A4:B4"].Style.KnownColor = ExcelColors.LightOrange;
sheet.Range["A5:B5"].Style.KnownColor = ExcelColors.LightTurquoise;
//Border
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeTop].LineStyle = LineStyleType.Thin;
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeBottom].LineStyle = LineStyleType.Thin;
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeLeft].LineStyle = LineStyleType.Thin;
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].Color = Color.FromArgb(0, 0, 128);
sheet.Range["A1:B5"].Style.Borders[BordersLineType.EdgeRight].LineStyle = LineStyleType.Thin;
//Number format
sheet.Range["B2:C5"].Style.NumberFormat = "\"$\"#,##0";
chart.PlotArea.Fill.Visible = false;
//Save the file
workbook.SaveToFile("Sample.xls");
//Launch the file
System.Diagnostics.Process.Start("Sample.xls");
}
}
}
Imports Spire.Xls
Imports System.Drawing
Module Module1
Sub Main()
'Create a new workbook
Dim workbook As New Workbook()
'Initialize worksheet
workbook.CreateEmptySheets(1)
Dim sheet As Worksheet = workbook.Worksheets(0)
'Set sheet name
sheet.Name = "Chart data"
'Set the grid lines invisible
sheet.GridLinesVisible = False
'Create a chart
Dim chart As Chart = sheet.Charts.Add(ExcelChartType.Pie3D)
'Set region of chart data
chart.DataRange = sheet.Range("B2:B5")
chart.SeriesDataFromRange = False
'Set position of chart
chart.LeftColumn = 1
chart.TopRow = 6
chart.RightColumn = 9
chart.BottomRow = 25
'Chart title
chart.ChartTitle = "Sales by year"
chart.ChartTitleArea.IsBold = True
chart.ChartTitleArea.Size = 12
'Set the chart
Dim cs As Spire.Xls.Charts.ChartSerie = chart.Series(0)
'Chart Labels resource
cs.CategoryLabels = sheet.Range("A2:A5")
'Chart value resource
cs.Values = sheet.Range("B2:B5")
'Set the value visible in the chart
cs.DataPoints.DefaultDataPoint.DataLabels.HasValue = True
'Year
sheet.Range("A1").Value = "Year"
sheet.Range("A2").Value = "2002"
sheet.Range("A3").Value = "2003"
sheet.Range("A4").Value = "2004"
sheet.Range("A5").Value = "2005"
'Sales
sheet.Range("B1").Value = "Sales"
sheet.Range("B2").NumberValue = 4000
sheet.Range("B3").NumberValue = 6000
sheet.Range("B4").NumberValue = 7000
sheet.Range("B5").NumberValue = 8500
'Style
sheet.Range("A1:B1").Style.Font.IsBold = True
sheet.Range("A2:B2").Style.KnownColor = ExcelColors.LightYellow
sheet.Range("A3:B3").Style.KnownColor = ExcelColors.LightGreen1
sheet.Range("A4:B4").Style.KnownColor = ExcelColors.LightOrange
sheet.Range("A5:B5").Style.KnownColor = ExcelColors.LightTurquoise
'Border
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeTop).Color = Color.FromArgb(0, 0, 128)
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeTop).LineStyle = LineStyleType.Thin
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeBottom).Color = Color.FromArgb(0, 0, 128)
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeBottom).LineStyle = LineStyleType.Thin
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeLeft).Color = Color.FromArgb(0, 0, 128)
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeLeft).LineStyle = LineStyleType.Thin
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeRight).Color = Color.FromArgb(0, 0, 128)
sheet.Range("A1:B5").Style.Borders(BordersLineType.EdgeRight).LineStyle = LineStyleType.Thin
'Number format
sheet.Range("B2:C5").Style.NumberFormat = """$""#,##0"
chart.PlotArea.Fill.Visible = False
'Save doc file.
workbook.SaveToFile("Sample.xls")
'Launch the MS Word file.
System.Diagnostics.Process.Start("Sample.xls")
End Sub
End Module

EXCEL Interior for C#, VB.NET
The sample demonstrates how to write excel 2007 workbook.

EXCEL Pagesetup for C#, VB.NET
The sample demonstrates how to work with page setup in an excel workbook.

EXCEL DataValidation for C#, VB.NET
The sample demonstrates how to write validation into spreadsheet.

