ASP.net How to Center Exported image in Excel

07-28-09, 01:30 AM
|
|
Newbie Coder
|
|
Join Date: Jul 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
ASP.net How to Center Exported image in Excel
Hi there,
I have to export some images to excel, To center the images I use a table and colspan=4 to merge four columns of excel file,
when I write a text to excel file it is easily get centered, but for image I tried every possible scenarios, like <img algin="center"> or <div align="center">
and even put it inside a table and <td align="center">, but the result is the same.
any ideas?
Thanks in advance
Last edited by Nico; 07-28-09 at 06:41 AM.
Reason: Changed font size.
|

08-01-09, 10:01 PM
|
 |
Community Leader
|
|
Join Date: Oct 2003
Location: Chicago, IL
Posts: 1,898
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
It's pretty hard to tell what you are trying to do without seeing the code. Can you post your code?
Thanks,
Pete
|

08-10-09, 03:56 AM
|
|
Newbie Coder
|
|
Join Date: Jul 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Export Image to Excel - How to Center the image?
Hi There,
I am trying to export an image to Excel file from Asp.Net C# page. My code is working fine, and I can merge let say 6 columns in excel file and put the image on these cells.
What I would like to know is how to Center this image in the middle of these merged cells?
I've already tried every possible solution came to my mind, Like I could have a table,add a <td colspan=6> and then try to center the image, not working,
second solution was to add a dynamically created div and add style to center the image, not working,
after searching for a long time,I could not find any proper answer,
any Ideas?
Thanks
|

08-10-09, 11:57 AM
|
 |
Community Leader
|
|
Join Date: Oct 2003
Location: Chicago, IL
Posts: 1,898
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
As I said above why don't you post the code you have so far? It will be a lot easier to help you.
Thanks,
Pete
|

08-11-09, 12:58 AM
|
|
Newbie Coder
|
|
Join Date: Jul 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by digioz
As I said above why don't you post the code you have so far? It will be a lot easier to help you.
Thanks,
Pete
|
Thanks for your reply, here is my code:
Code:
protected void btnExportExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=file.xls");
Response.ContentType = "application/vnd.ms-excel";
String imagepath = "<img src='http://localhost:56705/WebSite12/images/logo.jpg' width='70' height='80'/>";
Response.Output.Write("\n<body>\n<html>");
Response.Output.Write("<table width='800' align='center' style='text-align:center'");
Response.Output.Write("<tr><td colspan='10' align='center'><div align='center' style='text-align:center'>" + imagepath + "</div></td></tr>");
Response.Output.Write("</table>");
Response.Output.Write("\n</body>\n</html>");
Response.Flush();
Response.End();
}
Last edited by digioz; 08-11-09 at 10:35 AM.
Reason: Please use Code Tags.
|

08-11-09, 10:53 AM
|
 |
Community Leader
|
|
Join Date: Oct 2003
Location: Chicago, IL
Posts: 1,898
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Hi,
The correct way to write to excel file is using Office Interop. You will need to reference the Microsoft Excel Object Library.
For example:
Code:
Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;
try
{
//Start Excel and get Application object.
oXL = new Excel.Application();
oXL.Visible = true;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Add table headers going cell by cell.
oSheet.Cells[1, 1] = "First Name";
oSheet.Cells[1, 2] = "Last Name";
oSheet.Cells[1, 3] = "Full Name";
oSheet.Cells[1, 4] = "Salary";
//Format A1:D1 as bold, vertical alignment = center.
oSheet.get_Range("A1", "D1").Font.Bold = true;
oSheet.get_Range("A1", "D1").VerticalAlignment =
Excel.XlVAlign.xlVAlignCenter;
// Create an array to multiple values at once.
string[,] saNames = new string[5,2];
saNames[ 0, 0] = "John";
saNames[ 0, 1] = "Smith";
saNames[ 1, 0] = "Tom";
saNames[ 1, 1] = "Brown";
saNames[ 2, 0] = "Sue";
saNames[ 2, 1] = "Thomas";
saNames[ 3, 0] = "Jane";
saNames[ 3, 1] = "Jones";
saNames[ 4, 0] = "Adam";
saNames[ 4, 1] = "Johnson";
//Fill A2:B6 with an array of values (First and Last Names).
oSheet.get_Range("A2", "B6").Value2 = saNames;
//Fill C2:C6 with a relative formula (=A2 & " " & B2).
oRng = oSheet.get_Range("C2", "C6");
oRng.Formula = "=A2 & \" \" & B2";
//Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRng = oSheet.get_Range("D2", "D6");
oRng.Formula = "=RAND()*100000";
oRng.NumberFormat = "$0.00";
//AutoFit columns A:D.
oRng = oSheet.get_Range("A1", "D1");
oRng.EntireColumn.AutoFit();
//Manipulate a variable number of columns for Quarterly Sales Data.
DisplayQuarterlySales(oSheet);
//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
oXL.Visible = true;
oXL.UserControl = true;
}
catch( Exception theException )
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat( errorMessage, theException.Message );
errorMessage = String.Concat( errorMessage, " Line: " );
errorMessage = String.Concat( errorMessage, theException.Source );
MessageBox.Show( errorMessage, "Error" );
}
Pete
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|