在Outlook 2013邮件正文中复制粘贴excel范围 [英] Copy paste excel range in outlook 2013 mail body
问题描述
Hello All,
我有一份包含3张数据的excel文件。
我需要复制第1页的数据(这是一个格式化的表格)并将其粘贴到Outlook的邮件正文中。
我尝试了以下从网上找到的代码:
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:
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:
var result = File.ReadAllText(temporaryFilepath);
它说进程无法访问C:\\中的文件.... tempfile.html因为它正被另一个进程使用。
我想要粘贴的数据也在表1中,但是我们在哪里提到它?
谢谢...... !!
我尝试了什么:
i尝试了无效的示例代码。
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.
推荐答案
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.
之后wbk.SaveAs(..)你必须关闭它(wbk.Close)并处理Excel App。
After wbk.SaveAs(..) you have to close it (wbk.Close) and dispose of the Excel App.
这篇关于在Outlook 2013邮件正文中复制粘贴excel范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!