<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://weblogs.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Mecit Atmaca : C#</title><link>http://weblogs.asp.net/mecitatmaca/archive/tags/C_2300_/default.aspx</link><description>Tags: C#</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Server-side Office Integration -7 (Conclusion)</title><link>http://weblogs.asp.net/mecitatmaca/archive/2008/11/03/server-side-office-integration-7-conclusion.aspx</link><pubDate>Tue, 04 Nov 2008 04:28:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6719020</guid><dc:creator>atmacam</dc:creator><author>atmacam</author><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/mecitatmaca/rsscomments.aspx?PostID=6719020</wfw:commentRss><comments>http://weblogs.asp.net/mecitatmaca/archive/2008/11/03/server-side-office-integration-7-conclusion.aspx#comments</comments><description>&lt;P&gt;Voow, I am almost done. On this Sunday night, at Panera, I have enjoyed writing while listening to the jazz and drinking coups of tea (3, so far). &lt;/P&gt;
&lt;P&gt;Alright, let's summarize briefly what we have done so far in these series: After giving some intro, In this complete but simple web application, we import data from an excel spreadsheet uploaded by the user into a database table by leveraging .NET 3.5 framework and ExcelPackage with C#. &lt;/P&gt;
&lt;P&gt;Plus, we expose to some other areas of application development: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Best practices and Enterprise Library (used Data and Logging Blocks). Later on, I will deep into more details hopefully &lt;/LI&gt;
&lt;LI&gt;Object serialization and de-serializaition &lt;/LI&gt;
&lt;LI&gt;File operations using System.IO namespace (FileInfo, Stream/FileStream) &lt;/LI&gt;
&lt;LI&gt;Resource releases/disposal once it is done by using 'using' blocks &lt;/LI&gt;
&lt;LI&gt;XML Document &lt;/LI&gt;
&lt;LI&gt;Use of XPATH for accessing XML content. &lt;/LI&gt;
&lt;LI&gt;Use XML Document as way of importing data (like a Batch process) &lt;/LI&gt;
&lt;LI&gt;T-SQL Script for reading data from an XML Document &lt;/LI&gt;
&lt;LI&gt;Rendering data as an Office document &lt;/LI&gt;
&lt;LI&gt;Use of Data Controls. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;I will also publish the solution. Please note that this is just a sample application, I have used very simple development architecture (UI and Business), it is not optimized nor tested well.&amp;nbsp;&amp;nbsp; &lt;/P&gt;
&lt;P&gt;I think I am done. Bye for now &amp;amp; All the Best!. &lt;/P&gt;
&lt;DIV class=wlWriterSmartContent id=scid:0767317B-992E-4b12-91E0-4F059A8CECA8:041b0360-6240-49c5-9a45-10dc7b8a7b70 style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;Technorati Tags: &lt;A href="http://technorati.com/tags/Best%20Practices%20&amp;amp;%20Design" rel=tag mce_href="http://technorati.com/tags/Best%20Practices%20&amp;amp;%20Design"&gt;Best Practices &amp;amp; Design&lt;/A&gt;,&lt;A href="http://technorati.com/tags/Office%20Integration" rel=tag mce_href="http://technorati.com/tags/Office%20Integration"&gt;Office Integration&lt;/A&gt;,&lt;A href="http://technorati.com/tags/How%20to%20import%20excel%20spreadsheet%20into%20a%20database%20table" rel=tag mce_href="http://technorati.com/tags/How%20to%20import%20excel%20spreadsheet%20into%20a%20database%20table"&gt;How to import excel spreadsheet into a database table&lt;/A&gt;,&lt;A href="http://technorati.com/tags/Data%20Application%20Block" rel=tag mce_href="http://technorati.com/tags/Data%20Application%20Block"&gt;Data Application Block&lt;/A&gt;,&lt;A href="http://technorati.com/tags/Validation%20Block" rel=tag mce_href="http://technorati.com/tags/Validation%20Block"&gt;Validation Block&lt;/A&gt;,&lt;A href="http://technorati.com/tags/System.IO.Packaging." rel=tag mce_href="http://technorati.com/tags/System.IO.Packaging."&gt;System.IO.Packaging.&lt;/A&gt;&lt;/DIV&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6719020" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/Agile/default.aspx">Agile</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/AJAX/default.aspx">AJAX</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/Biztalk/default.aspx">Biztalk</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/CLR/default.aspx">CLR</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/Design+Patterns/default.aspx">Design Patterns</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/LINQ/default.aspx">LINQ</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/SOA/default.aspx">SOA</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/SQL+Server/default.aspx">SQL Server</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/WCF/default.aspx">WCF</category></item><item><title>Server-side Office Integration -3: Use of Excel Package for generating &amp; updating Excel from the server side</title><link>http://weblogs.asp.net/mecitatmaca/archive/2008/11/02/server-side-office-integration-3-use-of-excel-package-for-generating-amp-updating-excel-from-the-server-side.aspx</link><pubDate>Mon, 03 Nov 2008 00:58:00 GMT</pubDate><guid isPermaLink="false">c06e2b9d-981a-45b4-a55f-ab0d8bbfdc1c:6716553</guid><dc:creator>atmacam</dc:creator><author>atmacam</author><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://weblogs.asp.net/mecitatmaca/rsscomments.aspx?PostID=6716553</wfw:commentRss><comments>http://weblogs.asp.net/mecitatmaca/archive/2008/11/02/server-side-office-integration-3-use-of-excel-package-for-generating-amp-updating-excel-from-the-server-side.aspx#comments</comments><description>&lt;H3&gt;So What is ExcelPackage really?&lt;/H3&gt;
&lt;P&gt;In short, it is to create and/or modify Excel spreadsheets from the server. To me, it is an open source API that includes classes and &lt;STRONG&gt;wrappers&lt;/STRONG&gt; around &lt;STRONG&gt;.NET 3.0 System.IO.Packaging&lt;/STRONG&gt; class library according to new &lt;STRONG&gt;Open XML&lt;/STRONG&gt; standards. It is called Package because it contains multiple data objects, Worksheets, Worksheet, Row, Cell, Column, Styles, etc... A good example would be Zip file. It is aimed to be a complete solution, however it is still in progress, applying styles, rows cloning, etc waiting for heroes... -Hey it is open source... Thanks &lt;STRONG&gt;Dr. John Tunnicliffe&lt;/STRONG&gt;, for his generous contribution.&lt;/P&gt;
&lt;H3&gt;How does it work?&lt;/H3&gt;
&lt;P&gt;If you look at the System.IO.Packaging namespace, you will see there are different ways of opening a Package. And I am particularly talking about one of them -opening a Package from a stream object:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://weblogs.asp.net/blogs/mecitatmaca/WindowsLiveWriter/OfficeIntegration3HowtomanipulateExcelda_11EA9/image_4.png" mce_href="http://weblogs.asp.net/blogs/mecitatmaca/WindowsLiveWriter/OfficeIntegration3HowtomanipulateExcelda_11EA9/image_4.png"&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=418 alt=image src="http://weblogs.asp.net/blogs/mecitatmaca/WindowsLiveWriter/OfficeIntegration3HowtomanipulateExcelda_11EA9/image_thumb_1.png" width=624 border=0 mce_src="http://weblogs.asp.net/blogs/mecitatmaca/WindowsLiveWriter/OfficeIntegration3HowtomanipulateExcelda_11EA9/image_thumb_1.png"&gt;&lt;/A&gt; &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Steps:&lt;/STRONG&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Dump the excel data into a FileStream typed object, &lt;/LI&gt;
&lt;LI&gt;Open a Package object on the stream object, &lt;/LI&gt;
&lt;LI&gt;Construct an Excel worksheet in the package at given index.&amp;nbsp; &lt;/LI&gt;
&lt;LI&gt;Now, we can start all kinds of CRUD (Create, Read, Update, Delete) operations on the worksheet for its rows, columns, cells, styles, properties. Easy right:) &lt;/LI&gt;&lt;/OL&gt;
&lt;TABLE class="" cellSpacing=0 cellPadding=2 width=580 border=1&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" vAlign=top width=580&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: gray"&gt;/// &amp;lt;summary&amp;gt;
/// &lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;Imports Excel spreadsheet into Database
&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;/// &amp;lt;/summary&amp;gt;
/// &amp;lt;param name="results"&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;The list of errors if any&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/param&amp;gt;
/// &amp;lt;returns&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: green"&gt;Array of bytes of after excel saved&lt;/SPAN&gt;&lt;SPAN style="COLOR: gray"&gt;&amp;lt;/returns&amp;gt;
&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;public byte&lt;/SPAN&gt;[] ImportExcelData(&lt;SPAN style="COLOR: blue"&gt;out &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt; results)
{
    &lt;SPAN style="COLOR: #2b91af"&gt;DataTable &lt;/SPAN&gt;dt;
    _xmlDoc = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;XmlDocument&lt;/SPAN&gt;();
    _errors = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;();

    _bytes = &lt;SPAN style="COLOR: #2b91af"&gt;File&lt;/SPAN&gt;.ReadAllBytes(_fileName);
    &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(_bytes == &lt;SPAN style="COLOR: blue"&gt;null &lt;/SPAN&gt;|| _bytes.Length == 0)
        &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Exception&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"No data found in the file. "&lt;/SPAN&gt;);

    &lt;SPAN style="COLOR: #2b91af"&gt;FileInfo &lt;/SPAN&gt;fi = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;FileInfo&lt;/SPAN&gt;(_fileName);
    &lt;SPAN style="COLOR: green"&gt;//writes data into filestream object
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;using &lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;FileStream &lt;/SPAN&gt;fs = fi.OpenWrite())
    {
        fs.Write(_bytes, 0, _bytes.Length);
        fs.Flush();
    }
    
    &lt;SPAN style="COLOR: green"&gt;//create ExcelPackage with fileinfo object created above
    &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;using &lt;/SPAN&gt;(_xlPackage = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ExcelPackage&lt;/SPAN&gt;(fi))
    {
        &lt;SPAN style="COLOR: green"&gt;//initialize the excel worksheet object from the excel package at given index.
        &lt;/SPAN&gt;_xlWS = _xlPackage.Workbook.Worksheets[_sheetIndex];

        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(_xlWS == &lt;SPAN style="COLOR: blue"&gt;null &lt;/SPAN&gt;|| !_xlWS.WorksheetXml.HasChildNodes)
            &lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Exception&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"No worksheet found at given index."&lt;/SPAN&gt;);

        &lt;SPAN style="COLOR: green"&gt;//transforms ExcelWorksheet context to Xml document.
        &lt;/SPAN&gt;TranslateXlToXmlDocument();
        &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(_errors.Count == 0)
        {
            &lt;SPAN style="COLOR: green"&gt;//inserts XML document into database table
            &lt;/SPAN&gt;dt = InsertXmlData();
            &lt;SPAN style="COLOR: green"&gt;//syncs the excel worksheet with database data
            &lt;/SPAN&gt;UpdateXlWS(dt);
            &lt;SPAN style="COLOR: green"&gt;//accepts changes made to excel worksheet --&amp;gt; excel package.
            &lt;/SPAN&gt;_xlPackage.Save();
        }
    }
    fi.Refresh();
    &lt;SPAN style="COLOR: blue"&gt;byte&lt;/SPAN&gt;[] updatedData = (_errors.Count == 0) ? &lt;SPAN style="COLOR: #2b91af"&gt;File&lt;/SPAN&gt;.ReadAllBytes(fi.FullName) : &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;;
   
    results = _errors;
    &lt;SPAN style="COLOR: blue"&gt;return &lt;/SPAN&gt;updatedData;
}&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;DIV class=wlWriterSmartContent id=scid:0767317B-992E-4b12-91E0-4F059A8CECA8:5581eb96-e9f1-48a1-ae03-7f33d7062c53 style="PADDING-RIGHT: 0px; DISPLAY: inline; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-TOP: 0px"&gt;Technorati Tags: &lt;A href="http://technorati.com/tags/.NET%203.0/3.5" rel=tag mce_href="http://technorati.com/tags/.NET%203.0/3.5"&gt;.NET 3.0/3.5&lt;/A&gt;,&lt;A href="http://technorati.com/tags/C#" rel=tag mce_href="http://technorati.com/tags/C#"&gt;C#&lt;/A&gt;,&lt;A href="http://technorati.com/tags/OpenXML" rel=tag mce_href="http://technorati.com/tags/OpenXML"&gt;OpenXML&lt;/A&gt;,&lt;A href="http://technorati.com/tags/Excel%20Package" rel=tag mce_href="http://technorati.com/tags/Excel%20Package"&gt;Excel Package&lt;/A&gt;,&lt;A href="http://technorati.com/tags/FileInfo" rel=tag mce_href="http://technorati.com/tags/FileInfo"&gt;FileInfo&lt;/A&gt;&lt;/DIV&gt;&lt;img src="http://weblogs.asp.net/aggbug.aspx?PostID=6716553" width="1" height="1"&gt;</description><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/.NET+3.0_2F00_3.5/default.aspx">.NET 3.0/3.5</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/AJAX/default.aspx">AJAX</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/Application+Blcoks/default.aspx">Application Blcoks</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/ExcelPackage/default.aspx">ExcelPackage</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/Import+Excel+to+Database/default.aspx">Import Excel to Database</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/Open+Source/default.aspx">Open Source</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/OpenXML/default.aspx">OpenXML</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/XML/default.aspx">XML</category></item></channel></rss>