<?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 : .NET 3.0/3.5</title><link>http://weblogs.asp.net/mecitatmaca/archive/tags/.NET+3.0_2F00_3.5/default.aspx</link><description>Tags: .NET 3.0/3.5</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><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/C_2300_/default.aspx">C#</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/AJAX/default.aspx">AJAX</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/XML/default.aspx">XML</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/.NET+3.0_2F00_3.5/default.aspx">.NET 3.0/3.5</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/OpenXML/default.aspx">OpenXML</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/ExcelPackage/default.aspx">ExcelPackage</category><category domain="http://weblogs.asp.net/mecitatmaca/archive/tags/Application+Blcoks/default.aspx">Application Blcoks</category></item></channel></rss>