Pages

Thursday, October 24, 2013

ASP.net Export to excel.

Hello everyone,
 
Today I  am explaining about different approaches for exporting data from ASP.Net web page to Excel.
 
So, there are 3 approaches by which we can export data to excel.
  1. Export using HTML writer.
  2. Export With the help of Telerik controls.
  3. Export using Microsoft.Office.Interop.Excel.
 
1. Export using HTML writer: very simple approach but the problem is you can export, but you can not import using OLEDB connecting. If you just want to export data and you are sure that it is never going to be imported than you can use this approach.

2. Export With the help of Telerik controls:
This approach is also very good approach, but limitation is, If you want more than one sheet in out file then as per my understanding it is not possible.
  
 3.Export using Microsoft.Office.Interop.Excel:
This approach works always but need little hard work.
 
 
 
 
For more details on this you can ask questions in comment section. 
 
Related code will post soon on this blog Keep tune :)

Friday, October 18, 2013

Read Excel Cell value using OLEDB connection.

string documentPath = ConfigurationManager.AppSettings["SiaTempPath"] + "SIATemp.xlsb";

  string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + documentPath +

";Extended Properties=\"Excel 12.0;IMEX=1;HDR=NO;TypeGuessRows=0;ImportMixedTypes=Text\"";

OleDbConnection connection = new OleDbConnection(connectionString);
 

// open connection to excel file
connection.Open();

OleDbCommand cmd = new OleDbCommand();

OleDbDataAdapter adapter = new OleDbDataAdapter();

DataSet ds = new DataSet();



// webconfig entry: <add key="SiaCellCoordinates" value="D19" /> string siaCoordinate = ConfigurationManager.AppSettings["SiaCellCoordinates"].ToString();

cmd.CommandText = "SELECT * FROM [SIA$" + siaCoordinate + ":" + siaCoordinate + "]";



cmd.Connection = connection;
// code to assign command for OleDbDataAdapter



adapter.SelectCommand = cmd;
ds.Tables.Add("xlsImport", "Excel");

adapter.Fill(ds, "xlsImport");

//Required Value
ds.Tables["xlsImport"].Rows[0][0].ToString()


______________________________________________________________
Enjoy Coding ;-)

Wednesday, October 16, 2013

File upload control as read only

I want my File upload control as read only, so that no one can edit file upload text box. However I could select file using browse button. Write code

<asp:fileupload id="FileUpload1" runat="server" onkeydown="javascript:return false;" xmlns:asp="#unknown" />