Before that I request to you all please give your valuable feed back about the blog and this post. It will help me to create upcoming blog post better than now.
In
this example I want to explain how to read data from Excel file and save it as
XML. for that I will use LinqToExcel
C# Linq to Excel
LinqToExcel invited by Paul
Yoder is a .NET library which facilitates querying Excel spreadsheets and
.CSV files using the familiar LINQ syntax. So if you have idea in Linq then you
can easily use it. It is common for both xls, xlsx, csv file.
How to Install
We
can easily install this using Nuget Package by using following comets.
PM>
Install-Package LinqToExcel
|
Now our application is ready to use the
feature of Linq to Excel. Before go to c# code we need to prepare excel file using bellow employee details
EmployeeName
|
Address
|
City
|
State
|
Country
|
Name1
|
Address1
|
Chennai
|
Tamil
Nadu
|
India
|
Name2
|
Address2
|
Bangalore
|
Karnataka
|
India
|
Name3
|
Address3
|
Delhi
|
Delhi
|
India
|
Name4
|
Address4
|
Thiruvananthapuram
|
Kerala
|
India
|
Name5
|
Address5
|
Hyderabad
|
Telangana
|
India
|
using this above data and converted it into excel book like and name it as EmployeeWorkBook.xlsx.It look like bellow.
Now open the Visual studio and create a console application for read data from excel file, name it as
ExceltoXml. Let us see how read data from employee details excel
file using Linq to Excel and convert it into XML.
Rewrite Program.cs class file like bellow.
using LinqToExcel;
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
namespace ExcelToXml
{
class Program
{
static void Main(string[] args)
{
try
{
var excel = new
ExcelQueryFactory(@"E:\employeeworkbook.xlsx");
foreach (string sheetName in
excel.GetWorksheetNames())
{
//Get Excel column
var columnList =
excel.GetColumnNames(sheetName);
XDocument srcTree = new
XDocument(new XComment("This is a xml contain Employee Details"), new XElement("EmployeeRoot",
excel.Worksheet(sheetName).AsEnumerable().Select(a=> new XElement("Employees",from cn in columnList select new
XElement(cn,a[cn])))));
srcTree.Save(@"E:\employee.xml");
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
|
Save it and Run the application .for run the application pres F5 button from your key board.
It will create employee.xml file and save it in E Directory.
- <!--
This is a xml
contain Employee Details
-->
-<EmployeeRoot>
- <Employees>
- <Employee>
<EmployeeName>Name1</EmployeeName>
<Address>Address1</Address>
<City>Chennai</City>
<State>Tamil
Nadu</State>
<Country>India</Country>
</Employee>
- <Employee>
<EmployeeName>Name2</EmployeeName>
<Address>Address2</Address>
<City>Bangalore</City>
<State>Karnataka</State>
<Country>India</Country>
</Employee>
- <Employee>
<EmployeeName>Name3</EmployeeName>
<Address>Address3</Address>
<City>Delhi</City>
<State>Delhi</State>
<Country>India</Country>
</Employee>
- <Employee>
<Address>Address4</Address>
<City>Thiruvananthapuram</City>
<State>Kerala</State>
<Country>India</Country>
</Employee>
- <Employee>
<EmployeeName>Name5</EmployeeName>
<Address>Address5</Address>
<City>Hyderabad</City>
<State>Telangana</State>
<Country>India</Country>
</Employee>
</Employees>
</EmployeeRoot>
|
You have the saving of the file in a loop. If you were to download / use a spreadsheet with multiple pages, you would overwrite your previous entry every time you looped. Besides that good post. It was exactly what I was looking for.
ReplyDeleteNice information about converting excel to xml.
ReplyDeleteConvert ASP to ASP.Net
This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
ReplyDeletebest rpa training in bangalore
rpa training in bangalore | rpa course in bangalore
RPA training in bangalore
rpa training in chennai
rpa online training
This comment has been removed by the author.
ReplyDeleteInformation provided by you is very helpful and informative. Keep On updating such information.
ReplyDeleteangular js training in chennai
angular js training in tambaram
full stack training in chennai
full stack training in tambaram
php training in chennai
php training in tambaram
photoshop training in chennai
photoshop training in tambaram
This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
ReplyDeleteweb designing training in chennai
web designing training in omr
digital marketing training in chennai
digital marketing training in omr
rpa training in chennai
rpa training in omr
tally training in chennai
tally training in omr
I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post.is article. Thanks for sharing nice article
ReplyDeleteoracle training in chennai
oracle training in annanagar
oracle dba training in chennai
oracle dba training in annanagar
ccna training in chennai
ccna training in annanagar
seo training in chennai
seo training in annanagar
Thanks for your informative article,Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
ReplyDeletehadoop training in chennai
hadoop training in porur
salesforce training in chennai
salesforce training in porur
c and c plus plus course in chennai
c and c plus plus course in porur
machine learning training in chennai
machine learning training in porur
slot siteleri
ReplyDeletekralbet
betpark
tipobet
betmatik
kibris bahis siteleri
poker siteleri
bonus veren siteler
mobil ödeme bahis
2W0
شركة تنظيف منازل بجازان d33Gw0R0Xd
ReplyDeleteشركة مكافحة النمل الابيض بالاحساء GaoffctXY0
ReplyDelete