Home
Services
Business Listings
Free Business Scan
Listings & Reviews Pricing
App Development
Blog
Contact
Search
Home
Services
Business Listings
Free Business Scan
Listings & Reviews Pricing
App Development
Blog
Contact
Blog
Blog
Techemistry Blog
Combine Excel Documents Using C#
3/15/2013 12:47:00 PM
--
Ted Krapf
<p>A client yesterday had a common problem, "Ted I have 100+ Excel documents that I need to be able to quickly search through, and I'd like to combine all those work books into a single .xlsx file so that I can just search in one document."<br /> <br /> </p> <p>Sounds simple enough right? The first thing we tried together was scouring the web for a VBA script to do this. Seems simple, get a list of the files in a directory, open each doc, and copy and paste its contents into the main document. Well after an hour of stumbling through VBA syntax, help files, blogs, etc, we just couldn't find one that would work. The best we found that worked would combine the all the worksheets of all the other documents into the main workbook as worksheets -- still leaving us having to manually copy and paste 100's of worksheets into one.<br /> <br /> </p> <p>We had also found an array of different commercial 'combine' tools that ranged from dollars to $1,000's. Needless to say, being a programmer, I wanted to see how I could do this myself and so I could share the solution with you!<br /> <br /> </p> <p>I had forgot that months ago I discovered the goodness that is <a href="https://code.google.com/p/linqtoexcel/" target="_blank">LinqToExcel</a> at <a href="http://www.codeplex.com" target="_blank">CodePlex</a>. I had found and used LinqToExcel for some basic Excel import functions I wanted to write. <br /> <br /> </p> <p>I had also tried numerous solutions for exporting to Excel from C#, and ended building up a simple OpenXML project/dll to handle my exports. Most of it was referenced from this great <a href="http://blogs.msdn.com/b/chrisquon/archive/2009/07/22/creating-an-excel-spreadsheet-from-scratch-using-openxml.aspx" target="_blank">OpenXML article on the MSDN Blogs</a>. (Thanks again Chris!)<br /> <br /> </p> <p>So back to the combine solution -- I created a simple Windows form app project in Visual studio that just had a textbox for copy/pasting in the folder of xlsx files that client wanted to combine, and a button to start the function.<br /> <br /> </p> <p>The trick to this combine method working is ensuring that the excel documents you are going to combine are:<br /> <br /> </p> <p>1.) In the same directory <br /> 2.) Have the exact same columns<br /> 3.) Have the exact same column headings (caps, spaces, etc count)<br /> 4.) The worksheet in each workbook is named the same<br /> <br /> </p> <p>First I create a class in my code that matches the columns of the excel document. <br /> <br /> </p> <p><span style="color: #00b050;"><strong>protected class ReconcillationReport</strong><br /> <strong>{<br /> public string ID { get; set; }<br /> public string Date_Submitted { get; set; }<br /> public string Commission { get; set; }<br /> public string First_Name { get; set; }<br /> public string Last_Name { get; set; }<br /> public string Home_Phone_Num { get; set; }<br /> public string Distributor_Name { get; set; }<br /> public string Account_Num { get; set; }<br /> public string Status { get; set; }<br /> public string Code { get; set; }<br /> public string START_DATE { get; set; }<br /> public string END_DATE { get; set; }<br /> }</strong></span></p> <p>Next I created the button click event, retrieve the source directory from the textbox (txtRecDir.Text), and copy the file names in that directory to a List<>. I then create a new ReconcillationReport List<>. Next I loop through all the files in the directory listing (skipping any files called "WOOT" in case a combine was already performed on these files. <br /> <br /> </p> <p>Then using LinqToExcel's ExcelQueryFactory, I import each workbook to be combine one at a time and serialize them into a ReconcillationReport object, which I then add to the List of objects.<br /> <br /> </p> <p>I then simply convert the List of reports into a DataSet table, and use my export library to export the final combined Excel document.<br /> <br /> </p> <p>Here is the button click event<br /> <br /> </p> <p><strong><span style="color: #00b050;">private void button1_Click(object sender, EventArgs e)<br /> {<br /> string path = txtRecDir.Text;<br /> List<string> files = Directory.GetFiles(path, "*.xlsx").ToList();<br /> <br /> List<reconcillationreport> rr = new List<reconcillationreport>();<br /> foreach (string f in files.Where(c => !c.Contains("WOOT")))<br /> {<br /> var excelDoc = new ExcelQueryFactory(f);<br /> List<reconcillationreport> r = (from c in excelDoc.Worksheet<reconcillationreport>("Reconciliation") select c).ToList();<br /> </reconcillationreport></reconcillationreport></reconcillationreport></reconcillationreport></string></span></strong><strong><span style="color: #00b050;"> rr.AddRange(r);</span></strong><strong><span style="color: #00b050;"><br /> }<br /> <br /> DataSet ds = new DataSet();<br /> ds.Tables.Add(Utils.ToDataTable(rr, "Combined"));<br /> ExcelExport.CreateExcelFileOriginal.CreateExcelDocument(ds, path + "\\" + "WOOT.xlsx");<br /> <br /> btnRecCombind.Text = "DONE";<br /> }<br /> <br /> </span></strong></p> <p>Obviously there is no error checking, and note that this methodology requires advanced knowledge of the structure of the excel documents, but I still thought I'd share it in case it is useful to you. I'm sure as the client's request evolves I'll probably add the flexibility of combining dissimilar excel files to each other, but initially this simple code snippet saved the day. My client has cut 2 hours of excel wrangling out of every business day now -- what's that worth? <br /> <br /> </p> <p>Till next time,<br /> <br /> </p> <p>TK</p> <p><br /> </p>
[
return to articles list
]
Recent Articles
Daily Tip - Update AddRemoveProgramsIcon (and other properties) in a Visual Studio Installer Project
6/11/2019 8:35:00 PM
--
Ted Krapf
ZeroSSL with IIS 10
5/3/2019 5:11:00 PM
--
Ted Krapf
MinerGate.com Review - Mixed Emotions
2/3/2018 6:44:00 AM
--
Ted Krapf
Account Breach Monitoring
9/20/2017 3:36:00 PM
--
Ted Krapf
DBML Visualizer VERY Slow - solved!
3/29/2017 8:15:00 PM
--
Ted Krapf
Poor Man's SQL Log Shipping (Kinda)
12/23/2016 9:54:00 PM
--
Ted Krapf
Excel Column Letter To Number.com
4/8/2016 3:06:00 PM
--
Ted Krapf
Easy & Powerful Ecommerce Solution
4/5/2016 7:58:00 PM
--
Ted Krapf
asp:Menu Flicker on Page_Load - Solved
7/13/2015 1:48:00 PM
--
Ted Krapf
Offline ClickOnce app with command line parameters
3/9/2015 9:46:00 PM
--
Ted Krapf