In a general enterprise application development, it will involve the generation of reports, and the format of the general report is generated in an Excel format. For the generation of various reports, it has always been the pain in the hearts of the programmer. Because every report is written for many programmers, it means to write a big break code, and some reports may be extremely complicated and irregular. At the time, the programmer will waste the programmer to write and debug these code. If there is any way to write the code as little as possible, and can achieve the generation of various Excel reports, let's transfer to the topic.
We have to generate an Excel report, in fact, we only need two things:
1. What is the style of the report, which field should be filled, which column should be filled, the font size, color, cell height, width, whether the cell is merged, whether it is cross-line, is a transverse report (so-called horizontal " Report, I have been customized as: an Excel report is a record that records or has a record and its associated record. If a order report is usually generated by a order head and its related order details, this is me Custom horizontal report) or longitudinal report (so-called longitudinal report, I have been customized as: A report is generated by a recordset, if you want to generate a report that records a class student situation, you have to put this class The student record must be exported to this report, this report is basically a statistical report, and a report may have tens of thousands of records or more. This is the longitudinal report of my customized report) and so on.
2. Data of the report, have a report style for us, we know which data should be filled in which data, specific data, we can behave in the form of DataTable or DataSet The form is expressive, as for how to use it, you can define it.
Now we know that generating an Excel report As long as you have style and data, you can generate a report you want. The generation is not difficult, but our difficult is to describe the style of this report, how to describe .
For the description of the style we can use two ways:
1. Directly use XML to describe that the primary description of this XML description file can be as follows (lifetime description, it is not very specified):
XML Version = "1.0" encoding = "UTF-8"?>
Datafont>
Rangestyle>
Ranges>
ExcelStyle>
Style>
This XML Node ExcelStyle's attribute sort describes the entire Excel's format is longitudinal or lateral (so-called longitudinal or lateral, I have explained above), and the attribute Space describes the row of each record of the report. How many lines are spaced. Ranges is the parent of all child nodes RangeStyle, which usually has a lot of Rangestyle nodes for a report. Rangestyle actually begins which cells that do not describe which cells, such as node Range define cell height, width, border width, color of colors. TitleCell is defined (because I am a longitudinal report, so there is a title "Title property, such as the name of the title, which cell is filled in this name. Node DATACELL defines which of the fields to help you know, from which unit begins to fill in, whether the cell is merged, aligned, and the interval between this field line and the line. Node Title Font defines the Title font properties, such as size, color, whether it is bold, whether it is bold, aligned. Node DataFont defines the properties of the data font, such as size, color, whether it is bold, whether it is bold, aligned. A Rangestyle node actually defines a field fill in. If your report is to fill in a lot of fields, there will be many Rangestyle nodes to describe. This XML style definition file can write a tool to generate, I have ready-made itself written by the build tool called ExcelStyle, but it is still very simple, it is still not convenient, but can generate the style I need, which one needs me to send Give you (I don't know how to provide download). 2. XML Description Files and Excel Templates The method of combining, for some reports, especially those horizontal reports, sometimes it is extremely complicated, then ready-made Excel template, with an Excel template, we can refer to Template to generate an XML style definition file, when you specifically generate reports, we just know which use of the Excel template is which, the XML style file is, then tie our data, then even more complex reports can also be simple. Generate.
Specific use, you can refer to the following test column code (declared Exceloperate, XMloperate is a few universal components I wrote, if I need I can send mail, where data I is described in the form of DATATABLE):
Sing system;
Using nunit.framework;
Using Exceloperate;
Using XMloperate;
Using system.data;
Using system.data.sqlclient;
Namespace TESTEXCEL
{
///
/// summary>
///
[TestFixTure]
Public Class Excelexport
{
[TEST]
Public void export () // Do not need to export data from Excel templates
{
String SQL = "SELECT TOP 100 * from Customers";
// Take 100 data
EXCELFAACTORY = New Excelfactory ();
/ / Define an Excelfactory without parameters
_Factory.createExcelwithOutTemplate ("c: //bbbbbb.xml", getDataable (sql)); // Load the defined style file bbbbbbbb.xml and just taken data getDataTable (SQL)
_Factory.saveExcel ("C: //bbbbbbbbbbbbbbbbbbb.xls");
/ / Save the exported file
}
[TEST]
Public void exportwithTemplate () // Data exported by Excel template
{
String SQL = "SELECT TOP 1 * from Customers";
/ / Take a data
Excelfactory _Factory = New Excelfactory ("c: //qstandardcustomer.xls");
// Import template QStandardCustomer.xls
_Factory.createExcel ("c: //qstandardcustomer.xml", getDataable (SQL));
// Load the defined style file qstandadrcustomer.xml and the data that is just taken GetDataTable (SQL)
_Factory.saveExcel ("c: //qreportstr.xls");
/ / Finally save the exported Excel file
}
Private DataTable GetDataTable (String SQL)
{
String myconnectionstring = "packet size = 4096; user ID = sa; data source = 172.28.200.98; initial catalog = northwind";
SqlConnection MyConnection = New SqlConnection (MyConnectionstring);
MyConnection.open ();
SqldataAdapter myadapter = new sqldataadapter ();
Sqlcommand mycommand = new sqlcommand (sql, myconnection);
Mycommand.commandtype = commandtype.text;
myadapter.selectcommand = mycommand;
DataSet DS = New Dataset ();
Myadapter.fill (DS);
MyConnection.Close ();
Return ds.tables [0];
}
}
}
It can be seen from the above code, and the general report is generated. A larger time to save time on the generation of reports, and you can put more time on business logic.