|
-
Nov 6th, 2006, 03:50 PM
#1
Copy table from Excel and paste as html??
I'm sending an email via SMTP and I want the HTML content of the email be the table from an excel document. I want to preserve the look and feel of it by converting it to HTML somehow....
how would i do that? (ie given a selection in excel, convert it to HTML?)
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Nov 6th, 2006, 06:59 PM
#2
Re: Copy table from Excel and paste as html??
Here's some code that reads in excel, formats to html, and writes to a file. I've got email code too, but I wasn't sure if you wanted to paste the html into the email, or add a html file as an attachment.
Code:
private void btnConvert_Click(object sender, System.EventArgs e)
{
try
{
//retrieve data from excel
DataTable dt = GetDataFromExcelSheet("c:\\new.xls","Sheet1",false);
//format data into html table
string html = GetHtmlFromDataTable(dt);
//write data to file
System.IO.StreamWriter writer = new System.IO.StreamWriter("C:\\Temp\\MyTable.html",false);
writer.Write(html);
writer.Flush();
writer.Close();
}
catch ( Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private DataTable GetDataFromExcelSheet(string excelPath, string sheetName,bool hasHeaderRow)
{
string hdr = "No";
if (hasHeaderRow) hdr = "Yes";
System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelPath +
";Extended Properties=\"Excel 8.0;HDR=" + hdr + ";IMEX=1\"");
System.Data.OleDb.OleDbDataAdapter da =
new System.Data.OleDb.OleDbDataAdapter("SELECT * FROM [" + sheetName + "$]", cnn);
DataSet ds = new DataSet();
cnn.Open();
da.Fill(ds,"ExcelSheet");
return ds.Tables[0];
}
private string GetHtmlFromDataTable(DataTable dt)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder("<TABLE>");
foreach (DataRow dr in dt.Rows)
{
sb.Append("<TR>");
foreach (object o in dr.ItemArray)
{
sb.Append("<TD>");
sb.Append(o as string);
sb.Append("</TD>");
}
sb.Append("</TR>");
}
sb.Append("</TABLE>");
return sb.ToString();
}
-
Nov 8th, 2006, 12:42 PM
#3
-
Nov 8th, 2006, 12:52 PM
#4
-
Nov 8th, 2006, 04:41 PM
#5
Re: Copy table from Excel and paste as html??
I think you have to interop for everything but queries (which blow in my opinion). You should add the close call, I just forgot. Sorry about the o as string thing, I'm pretty new to c#. What is the format on the cells with the weird date values?
-
Nov 8th, 2006, 09:21 PM
#6
Re: Copy table from Excel and paste as html??
 Originally Posted by wild_bill
I think you have to interop for everything but queries (which blow in my opinion). You should add the close call, I just forgot. Sorry about the o as string thing, I'm pretty new to c#. What is the format on the cells with the weird date values?
hmm well in C# I just set that cell's value2 property to something like "11/3/06" and when i read the value again in C#, it's an integer like 32564 
lol
oh and thanks again for the code
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Nov 9th, 2006, 10:39 AM
#7
Re: Copy table from Excel and paste as html??
I'm not that familiar with the interop stuff, is there a way to set the cell's format type programatically? So instead of having excel try to auto pick the format, you would set it to Date.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|