Export to Excel,Read only,Bold heading in c#

Posted by Raja_89 under ASP.NET on 3/13/2013 | Points: 10 | Views : 7017 | Status : [Member] | Replies : 4
Hai
I am exporting the data from sqlserver to excel .
Is it possible to bold the headings and the set the document only to be read only

Code:


SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Iwens_Database;User ID=a;Password=123;Integrated Security=SSPI;");
sqlquery = "SELECT E_id, E_Name, E_Address, E_Contact_No, E_Email, DOEn, E_noi, E_coursename, E_year, E_companyname, E_others, E_design, E_Selected, E_Followup FROM dbo.Enquiry_table";
SqlDataAdapter sda = new SqlDataAdapter(sqlquery, con);
DataTable dt = new DataTable();
sda.Fill(dt);
string attachment = "attachment; filename=enquiryed.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dt.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");

int i;
foreach (DataRow dr in dt.Rows)
{
tab = "";
for (i = 0; i < dt.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
}


Kindly highlight the change you have made




Responses

Posted by: Santhi on: 3/13/2013 [Member] Starter | Points: 25

Up
0
Down

Posted by: Johnw on: 3/13/2013 [Member] Starter | Points: 25

Up
0
Down
Hello,

Firstly, I export data from sqlserver to a datatable and then to Excel with setting bold heading and read-only mode.

            //Create Excel File

Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//Insert Data to Sheet
sheet.InsertDataTable(dataTable, true, 1, 1);
//Set Headings as Bold
int lastRow = dataTable.Rows.Count;
int lastCol = dataTable.Columns.Count;
sheet.Range[1, 1, 1, lastCol].Style.Font.IsBold = true;
//Set Sheet as Read-Only
sheet.Protect("123456", SheetProtectionType.LockedCells);


I use this commerical .NET Excel Library(https://github.com/spirecomponent/.NETExcelLibrary ) instead of Microsoft Office Interop.

Hope helpful


Not what, but how

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ayush Nautiyal on: 12/1/2015 [Member] Starter | Points: 25

Up
0
Down
I WANT TO MAKE NON EDITABLE 7 COLUMNS AND 10 ROWS.wHAT IS CODE FOR THAT? WHAT IS THE NAME SPACE OF "Workbook" AS YOU MENTIONED IN CODE

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 12/1/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
0
Down
@Ayush Nautiyal, please make a new post in the forum. The original question was asked on 3/12/2013 and today is 12/01/2015 a gap of almost 3 years (: . That will provide more visibility to your question.

Hope you understand

--
Thanks & Regards,
RNA Team

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response