Tech
Forums
Jobs
Books
Events
Videos
Live
More
Interviews
Certification
Training
Career
Members
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Assemblies
.NET Core
.NET Standard
Active Directory
ADO.NET
Agile Development
AJAX
Alexa Skills
Algorithms in C#
Android
Angular
Architecture
ArcObject
Artificial Intelligence
ASP.NET
ASP.NET Core
Augmented Reality
Aurelia
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blockchain
Bootstrap
Bot Framework
Business
C#
C# Corner
C, C++, MFC
Career Advice
Chapters
CIO
Cloud
COBOL.NET
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Cortana Development
Cryptocurrency
Cryptography
Crystal Reports
Current Affairs
Custom Controls
Cyber Security
Data Mining
Databases & DBA
Design Patterns & Practices
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
Expression Studio
F#
Files, Directory, IO
Games Programming
GDI+
General
Google Cloud
Google Development
Graphics Design
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Internet & Web
Internet of Things
Ionic
iOS
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
kotlin
Leadership
Learn .NET
LightSwitch
LINQ
Machine Learning
Microsoft 365
Microsoft Office
Microsoft Phone
Mobile Development
Multithreading
NetBeans
Networking
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Power BI
Printing in C#
Products
Progressive Web Apps
Project Management
Python
Q#
QlikView
R
React
Reports using C#
Robotics & Hardware
Ruby on Rails
Salesforce
Security
Servers
SharePoint
SignalR
Silverlight
Smart Devices
Software Testing
SQL Language
SQL Server
Startups
String in C#
Swift
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
WCF
Wearables
Web Development
Web Services
Web3
Windows 10
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML Standard
XML
XNA
XSharp
Register
Login
2
Answers
How To Format Table In The Body Of Mail
Raushan Raj
7y
313
1
Reply
I am using C# for my problem.
I have an Excel file which has many worksheets. From "Fisrt Sheet", I am looking for a character "x" which will be present in some cells of a specific column (occurence of "x" will be in one specific column only, in different cells of that column). I am looking for "x" and extracting the corresponding row's details in a generic list (with naming the headers of extracted field). Now, I have to send this generic list in "tabular format" in body of the mail via Outlook.
I am getting the output but not in proper format. My code is extracting the information but I can't see any table borders and also the table headers are missing under which the extracted data should be filled.
Table header information should be like this:
Column 1 - MemoName
Column 1 - Type
Column 1 - Ext
Column 1 - Seller
Column 1 - Warehouse
Also the header should be in black color and the extracted details in red.
Please help me with my problem
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.IO;
using
System.Data;
using
Excel = Microsoft.Office.Interop.Excel;
using
Outlook = Microsoft.Office.Interop.Outlook;
namespace
xlsm
{
class
New
{
static
void
Main(sting[] args)
{
sting st;
long
rCnt, cCnt;
long
rows = 0, columns = 0;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range rng;
xlApp =
new
Excel.Application();
xlWorkBook = xlApp.Workbooks.Open(@
"F:\Doc_Excel"
, 0,
true
, 5,
""
,
""
,
true
, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows,
"\t"
,
false
,
false
, 0,
true
, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets[
"First Sheet"
];
rng = xlWorkSheet.UsedRange;
rows = rng.Rows.Count;
columns = rng.Columns.Count;
List<Memo> lst =
new
List<Memo>();
for
(rCnt = 1; rCnt < rows; rCnt++)
{
for
(cCnt = 1; cCnt < columns; cCnt++)
{
if
((rng.Cells[rCnt, cCnt]
as
Excel.rng).Value2 !=
null
)
{
st = (rng.Cells[rCnt, cCnt]
as
Excel.rng).Value2.Tosting();
if
(st ==
"x"
)
{
Memo ms =
new
Memo();
ms.MemoName = (rng.Cells[rCnt, 1]
as
Excel.rng).Value2.Tosting();
ms.Type = (rng.Cells[rCnt, 2]
as
Excel.rng).Value2.Tosting();
ms.Ext = (rng.Cells[rCnt, 3]
as
Excel.rng).Value2.Tosting();
ms.Seller = (rng.Cells[rCnt, 4]
as
Excel.rng).Value2.Tosting();
ms.Warehouse = (rng.Cells[rCnt, 5]
as
Excel.rng).Value2.Tosting();
lst.Add(ms);
}
}
}
}
try
{
Outlook.Application oApp =
new
Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
StringBuilder sb =
new
StringBuilder();
sb.Append(
"<table>"
);
foreach
(var row
in
lst)
{
sb.Append(
"<tr>"
);
sb.Append(
"<td>"
+ row.MemoName +
"</td>"
);
sb.Append(
"<td>"
+ row.Type +
"</td>"
);
sb.Append(
"<td>"
+ row.Ext +
"</td>"
);
sb.Append(
"<td>"
+ row.Seller +
"</td>"
);
sb.Append(
"<td>"
+ row.Warehouse +
"</td>"
);
sb.Append(
"</tr>"
);
}
sb.Append(
"</table>"
);
oMsg.HTMLBody = sb.ToString();
oMsg.Subject =
"Memo contents as required."
;
Outlook.Recipients oRecims = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecims.Add(
"
[email protected]
"
);
oRecip.Resolve();
oMsg.Send();
oRecip =
null
;
oRecims =
null
;
oMsg =
null
;
oApp =
null
;
}
catch
(Exception ex)
{
}
xlWorkBook.close(
true
,
null
,
null
);
xlApp.Quit();
Marshal.ReleaseComObject(xlWorkSheet);
Marshal.ReleaseComObject(xlWorkBook);
Marshal.ReleaseComObject(xlApp);
}
}
public
class
Memo
{
public
string
MemoName {
get
;
set
; }
public
string
Type {
get
;
set
; }
public
string
Ext {
get
;
set
; }
public
string
Seller {
get
;
set
; }
public
string
Warehouse {
get
;
set
; }
}
Post
Reset
Cancel
Answers (
2
)
Next Recommended Forum
data bind to panel or container
Get value of IEnumerator from System.Object