Monday, December 26, 2011

#4 : Read and Write Text File

There are times that we need to create a text file specially if our systems want's to send or exchange data with other systems. Text file or .txt file extentions are one of the few standards in sending data. It can easily open in a notepad program.

In this blog, I just want to share some codes on how to create a text file and write data or open the file and read data contents. I am using the System.IO which is available in .Net library.

Here's the code in creating and writing a text data;

       Dim MyFile As System.IO.File
         Dim MyWriter As System.IO.StreamWriter
         MyWriter  =  MyFile .CreateText("C:\sample.txt")
         MyWriter.WriteLine("RFID,Client,JobNumber,ObjectNumber,ItemDescription,Notes")
         MyWriter.WriteLine("000000000000000000621211,Jones,62121,1,First painting,in Lobby")


The Writeline method writes the string in one line and the next string will follow on the next line while the Write method writes the string continuously.

Here's the code for opening and reading a text file;

     Dim MyFile as System.IO.File
      Dim MyReader as System.IO.StreamReader
      MyReader = MyFile.OpenText(“C:\sample.txt”)


That's it. Just a few lines of codes and we're done. You have now a text reader and writer for .Net.

Thanks again for reading my blog.

Happy Holidays and Happy Coding too!

Wednesday, December 14, 2011

#3 : OCR Reader

Optical Character Recognition or simply OCR enable us to read text on scanned or bitmap images. It is a significant tool to help us copy the text without rewriting it again. I know there are a lot of OCR software available but I just want to show you how to do it on your own.

We will be using the Microsoft Office Document Imaging (MODI) which is of course available on the Microsoft Office. MODI library is not installed by default. You have to install it by using the setup package of Microsoft Office. See screenshot below;

Select Add or Remove Features

Click on Office Tools and select Microsoft Office Document Imaging. After installing, it will now be available on your .Net reference library. Using your visual studio, add reference then select the COM tab and find Microsoft Office Document Imaging Library as shown;


That's it and now we're ready to code. You only need a few lines of code to use MODI. 

         Document doc = new Document();
            doc.Create("D:\ScanFiles\Sample.jpg");
            doc.OCR(MiLANGUAGES.miLANG_ENGLISH, true, true);
            string strText;

            foreach (MODI.Image image in doc.Images)
            {
                strText = image.Layout.Text;
            }

There you go you already have your own OCR software. For any questions, hit the comments. Thank you for reading. Happy coding!

Sunday, December 11, 2011

#2 : PDF Parser

Portable Document Format or PDF file has been widely use file format in exchanging data or information compiled in a  single document. However, there are times that we need to take the text contents of it and save it on our database. That is basically the purpose of this application and I called it myself as PDF Parser.

PDF Parser is a utility to read text from PDF files. It uses PDFBox, an open source Java Class library. You can download it here http://sourceforge.net/projects/pdfbox/. This is a cool Java Class library, easy to use and very helpful tool in data mining.

You only need to add IKVM.GNU.Classpath and PDFBox-0.7.3 in your project reference. Then you just need to put up the following lines of code;

                    PDDocument doc = PDDocument.load("E:\sample.pdf");
                    PDFTextStripper stripper = new PDFTextStripper();
                    sOutputString string;
                    sOutputString = (stripper.getText(doc));

By the way, I'm using C# but you can easily convert it to Vb. Here's the screenshot sample PDF parser that I did;














Check my demo asp.net PDF Parser online at http://utility.aerinet.com/

Saturday, December 10, 2011

#1: Reading MS Excel File

Microsoft Excel is one of the best software by Microsoft which is bundled in Microsoft Office. It is often used for calculating and summarizing data. MS Excel has been widely used in business and in schools. Excel data format are often used to exchange information from one system to another. This blog is all about how to read Excel data within the .Net application.

I created this blog to help developers for .Net applications. Its my way of sharing my knowledge and skills to anybody interested coding in .Net. This is my first blog, so I hope you like it.

We only need to add Microsoft.Office.Interop.Excel to our reference. It is found in the .Net component library. Check this out;


Next thing is to follow the code below;

First, select an Excel file. You can use the OpenDialog to get the file. But on this demo, I use a fixed path. After getting the Excel file to read, we need to create a DataTable where we store our data. Then we will specify the Excel file path and the worksheet number


               Dim oTable As New DataTable
               Dim oRow As DataRow

                Dim xlApp = New Excel.Application
                Dim xlWorkBook = xlApp.Workbooks.Open("E:\MyFiles\sample.xls")
                Dim xlWorkSheet = xlWorkBook.Worksheets(1)
                Dim range = xlWorkSheet.UsedRange

                For cCnt = 1 To range.Columns.Count
                    oTable.Columns.Add("Column" + cCnt.ToString)
                 Next              

                For rCnt = CInt(txbRowTo.Text) + 1 To range.Rows.Count
                    oRow = oTable.NewRow

                     For cCnt = 1 To range.Columns.Count
                        oRow("Column" + cCnt.ToString) = xlWorkSheet.Cells(rCnt, cCnt).value.ToString
                    Next

                    oTable.Rows.Add(oRow)

                Next
 
That's it and we're done. Its easy right? I am using the VB.Net code but you can easily convert this in C#. This code is very useful specially if we have multiple sources of data and we want it to compile in a single database. If you have questions or something to discuss with me, please email at mrleefh78@gmail.com
Thank you for reading my blog.  I hope it helps you in a little way. Happy coding and best of luck to your projects!