Hi Akram.19
Please refer below sample. You need to set the Password to the workbook.
For readonly use the SaveAs method and set the readonly to true.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
Namespace
C#
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using ClosedXML.Excel;
VB.Net
Imports Excel = Microsoft.Office.Interop.Excel
Imports System.Data
Imports System.Data.SqlClient
Code
C#
public Form1()
{
InitializeComponent();
this.BindDataGridView();
}
private void BindDataGridView()
{
string str = "data source=.\\SQL2019;initial catalog=Northwind;user id=sa;password=pass@123;";
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(str))
{
SqlDataAdapter sda = new SqlDataAdapter("SELECT TOP 5 CustomerID,ContactName,Address,City,Country FROM Customers", con);
sda.Fill(dt);
this.dataGridView1.DataSource = dt;
}
}
private void btnExportExcel_Click(object sender, EventArgs e)
{
string str = "data source=.\\SQL2019;initial catalog=Northwind;user id=sa;password=pass@123;";
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(str))
{
SqlDataAdapter sda = new SqlDataAdapter("SELECT TOP 5 CustomerID,ContactName,Address,City,Country FROM Customers", con);
sda.Fill(dt);
}
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
string sourcePath = "D:\\Raw.xlsx";
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(sourcePath);
string datestr = DateTime.Now.ToShortDateString();
string destinationPath = "D:\\" + "Name_" + datestr.Replace('/', '_') + ".xls";
string quote = "Test Name" + "_" + datestr;
string salesman = "test@test.com".ToUpper();
string customer = "Test Name".ToUpper();
int currentSheet = 1;
int cell = 0;
int rowsToStart = 19;
for (int row = 0; row < dt.Rows.Count; row++)
{
if (currentSheet <= xlWorkBook.Sheets.Count)
{
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[1];
}
else
{
var xlSheets = xlWorkBook.Sheets as Excel.Sheets;
xlWorkSheet = (Excel.Worksheet)xlSheets.Add(misValue, xlSheets[xlWorkBook.Sheets.Count]);
xlWorkSheet.Name = "Sheet" + currentSheet.ToString();
}
xlWorkSheet.get_Range("C10", "E10").Value2 = salesman;
xlWorkSheet.get_Range("C12", "E12").Value2 = customer;
xlWorkSheet.get_Range("C12", "E12").Font.Bold = true;
xlWorkSheet.get_Range("C12", "E12").Font.Bold = true;
xlWorkSheet.get_Range("H12", "I12").Value2 = DateTime.Now;
xlWorkSheet.get_Range("H10", "I10").Value2 = quote;
for (int column = 0; column < dt.Columns.Count; column++)
{
xlWorkSheet.Cells[cell + rowsToStart, column + 1] = "'" + dt.Rows[row][column].ToString();
}
cell++;
}
if (System.IO.File.Exists(destinationPath))
{
System.IO.File.Delete(destinationPath);
}
xlWorkBook.Password = "123";
xlWorkBook.SaveAs(destinationPath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, true, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
MessageBox.Show("Excel file created , you can find the file in " + destinationPath);
}
VB.Net
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim str As String = "data source=.\SQL2019;initial catalog=Northwind;user id=sa;password=pass@123;"
Dim dt As DataTable = New DataTable()
Using con As SqlConnection = New SqlConnection(str)
Dim sda As SqlDataAdapter = New SqlDataAdapter("SELECT TOP 5 CustomerID,ContactName,Address,City,Country FROM Customers", con)
sda.Fill(dt)
End Using
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim sourcePath As String = "D:\Raw.xlsx"
xlApp = New Excel.Application()
xlWorkBook = xlApp.Workbooks.Add(sourcePath)
Dim datestr As String = DateTime.Now.ToShortDateString()
Dim destinationPath As String = "D:\" & "Name_" & datestr.Replace("/"c, "_"c) & ".xls"
Dim quote As String = "Test Name" & "_" & datestr
Dim salesman As String = "test@test.com".ToUpper()
Dim customer As String = "Test Name".ToUpper()
Dim currentSheet As Integer = 1
Dim cell As Integer = 0
Dim rowsToStart As Integer = 19
For row As Integer = 0 To dt.Rows.Count - 1
If currentSheet <= xlWorkBook.Sheets.Count Then
xlWorkSheet = CType(xlWorkBook.Worksheets(1), Excel.Worksheet)
Else
Dim xlSheets = TryCast(xlWorkBook.Sheets, Excel.Sheets)
xlWorkSheet = CType(xlSheets.Add(misValue, xlSheets(xlWorkBook.Sheets.Count)), Excel.Worksheet)
xlWorkSheet.Name = "Sheet" & currentSheet.ToString()
End If
xlWorkSheet.get_Range("C10", "E10").Value2 = salesman
xlWorkSheet.get_Range("C12", "E12").Value2 = customer
xlWorkSheet.get_Range("C12", "E12").Font.Bold = True
xlWorkSheet.get_Range("C12", "E12").Font.Bold = True
xlWorkSheet.get_Range("H12", "I12").Value2 = DateTime.Now
xlWorkSheet.get_Range("H10", "I10").Value2 = quote
For column As Integer = 0 To dt.Columns.Count - 1
xlWorkSheet.Cells(cell + rowsToStart, column + 1) = "'" & dt.Rows(row)(column).ToString()
Next
cell += 1
Next
If System.IO.File.Exists(destinationPath) Then
System.IO.File.Delete(destinationPath)
End If
xlWorkBook.SaveAs(destinationPath, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, True, misValue, _
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
xlWorkBook.Password = "123"
xlWorkBook.Close(True, misValue, misValue)
xlApp.Quit()
MessageBox.Show("Excel file created , you can find the file in " & destinationPath)
End Sub
Private Sub BindDataGridView()
Dim str As String = "data source=.\SQL2019;initial catalog=Northwind;user id=sa;password=pass@123;"
Dim dt As DataTable = New DataTable()
Using con As SqlConnection = New SqlConnection(str)
Dim sda As SqlDataAdapter = New SqlDataAdapter("SELECT TOP 5 CustomerID,ContactName,Address,City,Country FROM Customers", con)
sda.Fill(dt)
DataGridView1.DataSource = dt
End Using
End Sub
Screenshot