Report Development using C # and Excel (1)

Source: Internet
Author: User

The current business tools such as Crystal Reports, ActiveReport and so on, have provided a flexible, powerful function, but for more specialized forms, especially domestic applications, are made up of a lattice, so if the line to a root painting is more trouble, However, none of these tools provides a tabular definition of report layout. A good choice is to VS2005 the client report (RDLC), but in a few ways it is not flexible enough, for example, the flexibility is limited, it is difficult to implement some complex logic on their own coding, to VS2005, for some still use VS2003 project is only envious. With Excel, the advantage is that the layout of the page is flexible, while using code to manipulate the data is more flexible and accurate.

Here is a simple example of opening a well-defined Excel file that is used as a template for a report, and then populating the template's designated grid with data to form a report that, in the web, can generate an Excel file on the server in the specified naming manner, and then transfer to the client. The client starts Excel to print.

Excel.Application m_objexcel = null;
Excel._workbook m_objbook = null;
Excel.Sheets m_objsheets = null;
Excel._worksheet m_objsheet = null;
Excel.Range m_objrange = null;
Object m_objopt = System.Reflection.Missing.Value;
Try
{
M_objexcel = new Excel.Application ();
M_objbook = M_objExcel.Workbooks.Open ("Z:\\book1.xls", m_ Objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objopt, m_objOpt, m_objOpt); > M_objsheets = (excel.sheets) m_objbook.worksheets;
M_objsheet = (excel._worksheet) (M_objsheets.get_item (1));
String[] info = new String[5] {"Cure", "num", "dahuzizyd.cnblogs.com", "Han", "On time Austinos Ding Fei whether the ox Ding Fei"};
M_objrange = M_objsheet.get_range ("B1", m_objopt);
m_objRange.Value = info[0];
M_objrange = M_objsheet.get_range ("E1", m_objopt);
m_objRange.Value = info[1];
M_objrange = M_objsheet.get_range ("B2", m_objopt);
m_objRange.Value = info[2];
M_objrange = m_objsheet.get_Range ("E2", m_objopt);
m_objRange.Value = info[3];
M_objrange = M_objsheet.get_range ("A3", m_objopt);
m_objRange.Value = info[4];
M_objexcel.displayalerts = false;
m_objBook.SaveAs ("Z:\\book2.xls", m_objopt, m_objopt,
m_objopt, m_objopt, m_objopt, Excel.XlSaveAsAccessMode.xlNoChange,
m_objopt, m_objopt, m_objopt, m_objopt);
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
Finally
{
M_objbook.close (m_objopt, m_objopt, m_objopt);
M_objExcel.Workbooks.Close ();
M_ Objexcel.quit ();
System.Runtime.InteropServices.Marshal.ReleaseComObject (m_objbook);
System.Runtime.InteropServices.Marshal.ReleaseComObject (m_objexcel);
M_objbook = null;
M_objexcel = null;
GC. Collect ();
}

As you can see, it's not too complicated to use Excel to do part of the coding, but the big problem is that when the report requirements change, you recompile the deployment, and it's a pain to Hard-code when the grid is so much more. Using Excel, however, gives us an option to weigh the pros and cons of multiple scenarios at development time, using the most appropriate project.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.