calling Excel Macro from C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello Everyone,

I exported a gridview to excel spreadsheet. I want to adjust the column
width of the excel spreadsheet.
Is it possible to create Excel macro from C# to adjust the column width of
the spreadsheet. Below is my code for exporting to excel

string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();

Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();

dgAgentActivity.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(dgAgentActivity);
frm.RenderControl(htw);

Any help will be greatly appreciated.

Thanks.
 
Vinki said:
Hello Everyone,

I exported a gridview to excel spreadsheet. I want to adjust the column
width of the excel spreadsheet.
Is it possible to create Excel macro from C# to adjust the column width
of
the spreadsheet. Below is my code for exporting to excel

string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();

Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();

dgAgentActivity.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(dgAgentActivity);
frm.RenderControl(htw);

Any help will be greatly appreciated.

Thanks.

You can use the Microsoft Office Primary Interop Assemblies. These allow
you to add a references which gives you access to COM components and let you
do just about anything with an office application.

Look up http://msdn2.microsoft.com/en-us/library/15s06t57(VS.80).aspx

Here you should find out which reference to add, and information about
classes and methods.
 
No, it's not possible using your technique. You are better off trying an
Interop approach or using the OWC.
--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless Author Plug
OWC Black Book 2nd Edition
Exclusively on www.lulu.com/owc
$19.99


Matt Brunell said:
Vinki said:
Hello Everyone,

I exported a gridview to excel spreadsheet. I want to adjust the column
width of the excel spreadsheet.
Is it possible to create Excel macro from C# to adjust the column width
of
the spreadsheet. Below is my code for exporting to excel

string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();

Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();

dgAgentActivity.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(dgAgentActivity);
frm.RenderControl(htw);

Any help will be greatly appreciated.

Thanks.

You can use the Microsoft Office Primary Interop Assemblies. These allow
you to add a references which gives you access to COM components and let
you do just about anything with an office application.

Look up http://msdn2.microsoft.com/en-us/library/15s06t57(VS.80).aspx

Here you should find out which reference to add, and information about
classes and methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top