CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2009
    Location
    Africa, Congo
    Posts
    6

    Question Printing w/ Excel

    Hi,

    I am designing a software program that will run my computer repair shop. I am trying to find the best way to print receipts without having to go and buy a tool like Crystal Reports.


    This is what I would like to do:
    1. Design the layout of my receipts using Microsoft Excel and save the excel file to the hard drive.
    2. When I press "Print" in my C# program I would like it to load up excel and open my receipt file from the hard drive.
    3. I would then like it to place data such as name, address, etc... in the correct cells so that the data lines up with my predesigned Excel file.
    4. Either press "Print" again inside Excel or have my program issue a print command to Excel to print automatically (if possible).
    I would then just leave Excel running minimized on my desktop all day, to help speed up the process. This is not going to be a heavly used solution. Probably only printing 4 or 5 receipts per hour.

    There may be another way of doing this but it is the only solution I could come up with that would allow me to easily print documents from my program without having to design an entire report printing class, or buy 3rd party reporting software.

    Can someone please tell me if this is even possible? If it is possible where would I start? Are there any articles that describe how to do what I'm looking for? I've looked around a bit but since I don't know what to look for I'm not finding much information.

    I don't wnat to include any unmanaged code in my application if possible. I'm looking for a easy solution however, I'm not sure how hard this is going to be.

    - Thanks!

  2. #2
    Join Date
    Sep 2000
    Location
    FL
    Posts
    1,452

    Re: Printing w/ Excel

    Depending on the design of your receipt, you may spend as much time programming to excel as you would using the printerdocument object.

    Code:
            System.Drawing.Font _printfont = new System.Drawing.Font("Arial", 10);
            System.Drawing.Font _normalfont = new System.Drawing.Font("Arial", 10);
            System.Drawing.Font _boldfont = new System.Drawing.Font("Arial", 10,System.Drawing.FontStyle.Bold);
    
            public void Print()
            {
                System.Drawing.Printing.PrintDocument AB = new System.Drawing.Printing.PrintDocument();
                AB.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(pd_PrintPage);
                AB.Print();
            }
    And here is the PrintHandler Code.

    Code:
            private void pd_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
            {
                Single linesPerPage = 0;
                Single xPos = 0;
                int count = 0;
                int leftMargin = ev.MarginBounds.Left;
                int rightMargin = ev.MarginBounds.Right;
                int topMargin = ev.MarginBounds.Top;
                string line = String.Empty;
                linesPerPage = ev.MarginBounds.Height / _printfont.GetHeight(ev.Graphics);
    
                ev.Graphics.DrawImage(MyUtilities.Functions.GetLogo(), 500, 10);
    
                xPos = leftMargin;
                while ((count < linesPerPage) && (HasMoreLines()))
                {
                    if (count == 0)
                    {
                        _printfont = _boldfont;
                    }
                    else
                    {
                        _printfont = _normalfont;
                    }
                    line = ReturnNextLine();
                    if (line.Contains(":::"))
                    {
                        line = line.Substring(3);
                        xPos = leftMargin;
                        this.printLine(topMargin, xPos, ref count, line, ev, false);
                    }
                    else
                    {
                        if (line.Contains(";;;"))
                        {
                            line = line.Substring(3);
                            xPos = (Single)(rightMargin - leftMargin) / 2;
                        }
                        else
                        {
                            xPos = leftMargin;
                        }
                        this.printLine(topMargin, xPos, ref count, line, ev, true);
                    }
                }
                ev.HasMorePages = HasMoreLines();
            }
            private void printLine(Single topMargin, Single xPos, ref int count, string Text, System.Drawing.Printing.PrintPageEventArgs ev, bool incrementCount)
            {
                Single yPos = 0;
                yPos = topMargin + count * _printfont.GetHeight(ev.Graphics);
                ev.Graphics.DrawString(Text, _printfont, System.Drawing.Brushes.Black, xPos, yPos);
                if (incrementCount)
                {
                    count += 1;
                }
            }
            private bool HasMoreLines()
            {
                if (_currentLineCount > _printText.Count)
                {
                    return false;
                }
                return true;
            }
            private string ReturnNextLine()
            {
                if (_currentLineCount > _printText.Count)
                {
                    return "";
                }
                string TS = _printText[_currentLineCount - 1];
                _currentLineCount += 1;
                return TS;
            }
    This code took a string in a specific format and printed it out. If there were 3 ";" or ";;;" it would print starting in the middle of the page. Using the PrintDocument is fairly simple, however, I will say that I originally killed a few (virtual) trees working on getting things just right.

  3. #3
    Join Date
    Apr 2009
    Location
    Africa, Congo
    Posts
    6

    Re: Printing w/ Excel

    I will be using standard size paper for printing not the small paper you get from places like Wal-Mart. I want the layout to look similar to an invoice with boxes around sections. I also need to make sure it prints correctly if the reciept is larger the one page.

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Printing w/ Excel

    Quote Originally Posted by KPC View Post
    There may be another way of doing this but it is the only solution I could come up with that would allow me to easily print documents from my program without having to design an entire report printing class, or buy 3rd party reporting software.

    Can someone please tell me if this is even possible? If it is possible where would I start? Are there any articles that describe how to do what I'm looking for? I've looked around a bit but since I don't know what to look for I'm not finding much information.
    There are probably different ways of doing. It has already been suggested that you print it yourself using GDI+ and the .NET Framework classes. If you are using Visual Studio you may find that some editions come with the Developer component of Crystal Reports embedded in the Visual Studio environment. I think some editions also have a Microsoft equivalent of Crystal Reports.

    Your approach is certainly possible. If you want to know how you can add data to an excel document search for ADO.NET Excel. There are few articles around the subject. Basically you can use standard ADO.NET classes to connect to an excel file and do all the editing you need.

    With regards to the printing you could probably have a look at Excel Automation or automating excel. There are again several articles about. The API is document reasonably well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured