Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

I have an excel file with data in 3 sheets.
I need to copy data from sheet 1 (which is a formatted table) and paste it into mail body in outlook.

I have tried the following code which I found from net:

C#
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
                   Microsoft.Office.Interop.Excel.Workbook wbk = xlapp.Workbooks.Open(@"D:\\" + file_name);
                   Microsoft.Office.Interop.Excel.Worksheet wsht = (Microsoft.Office.Interop.Excel.Worksheet)wbk.Worksheets.get_Item(1);
                   

                   MemoryStream ms=new MemoryStream();
                  
                   var temporaryFilepath = Path.ChangeExtension(Path.GetTempFileName(), ".html");
                   wbk.SaveAs(temporaryFilepath, Microsoft.Office.Interop.Excel.XlFileFormat.xlHtml);

                   var result = File.ReadAllText(temporaryFilepath);

                   ms.Position=0;
                   StreamReader sr=new StreamReader(ms);
                   string summary=sr.ReadToEnd();

                   body=body+"<p>" + summary + "</p>";



Error is in this line:
C#
var result = File.ReadAllText(temporaryFilepath);


It says "the process cannot access the file in C:\\....tempfile.html because it is being used by another process."
Also the data I want to paste is in sheet 1 but here where do we mention that??

Thank you...!!

What I have tried:

i have tried sample code which is not working.
Posted
Updated 3-Apr-16 23:30pm
Comments
ZurdoDev 1-Apr-16 8:24am    
The error means you already have the excel file open. Make sure it is closed when you run the code.
FARONO 1-Apr-16 8:50am    
Tried to post as Solution, but website gives "busy" error:

After wbk.SaveAs(..) you have to close it (wbk.Close) and dispose of the Excel App.

C#
Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();
                   Microsoft.Office.Interop.Excel.Workbook wbk = xlapp.Workbooks.Open(@"D:\\" + file_name);
                   Microsoft.Office.Interop.Excel.Worksheet wsht = wbk.Worksheets[1];
                   Microsoft.Office.Interop.Excel.Range source = wsht.UsedRange;

                   int lastrow = source.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row;

                   body=body+"<table border='1' bordercolor='solid black'>";
                   for (int i = 1; i <= lastrow;i++)
                   {
                       string bgcolor = "", fontcolor="";

                       if(i==1)    //blue
                       {
                           bgcolor = "#1885CB";
                           fontcolor = "#FDFDFD";
                       }
                       else if (i ==lastrow)   //orange
                       {
                           bgcolor = "#E85815";
                           fontcolor = "#020000";
                       }
                       else   //white
                       {
                           bgcolor = "#FDFDFD";
                           fontcolor = "#020000";
                       }
                       string col1=wsht.Range["A" + i].Text.ToString();
                       string col2 = wsht.Range["B" + i].Text.ToString();
                       body = body + "<tr><td align='center' bgcolor='" + bgcolor + "' width='100px'><font color='" + fontcolor + "' face='Calibri'>" + col1 + "</font></td><td align='center' bgcolor='" + bgcolor + "' width='100px'><font color='" + fontcolor + "' face='Calibri'>" + col2 + "</font></td></tr>";
                   }
                   body = body + "</table></br>";

                   wbk.Close();
                   wbk = null;
                   xlapp.Quit();
                   xlapp = null;


Since the body of the mail also changes on condition.
 
Share this answer
 
v2
Comments
FARONO 4-Apr-16 5:37am    
You have to be careful with "LastRow" in Excel. If you (or the "user") drags formatting, styles or fonts for example over a large amount of cells, it will see all this cells as "range", even if it doesn't contain data.
Better is to define a "mandatory column"(which should always contain something) and then do a "Find" on the first empty cell.. Even better would be from both sides, so from "LastRow +1" search up for first filled cell.

Why don't you use the "HtmlCell/Table/Row" object by the way? save a lot of "strings"..
planetz 4-Apr-16 5:44am    
got it..!!
After wbk.SaveAs(..) you have to close it (wbk.Close) and dispose of the Excel App.
 
Share this answer
 
Comments
planetz 4-Apr-16 0:03am    
Thank you Farono....!! It was an error I should have understood..!! Now it's gone but anyway the excel range is not getting pasted yet. So I am not marking your solution as answer because it may mislead users, since question was to paste excel range in outlook mail body.
FARONO 4-Apr-16 3:36am    
How is the "result" ending up in the MemoryStream? You create a new MemoryStream (which makes 'Position=0' irrelevant), you read the contents of the converted html in 'result', but from there you start reading the memorystream 'ReadToEnd' but the ms is not defined..

You should put the html in the Stream.. (I'm not going to tell all of it, figure it out..) ;-)
planetz 4-Apr-16 5:24am    
For now, I have thought of and implemented a second way and it is working fine. I just looped through the excel rows and created html table rows accordingly on the fly. I will post the code, if it helps someone. And I will surely go through your hint. Thanks once again.
FARONO 4-Apr-16 5:26am    
That's even better, gives you control over the Css also.. :-)
planetz 4-Apr-16 5:32am    
Yes, it does... :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900