Simple Excel export is good to do, as long as set the table header, the loop in the table to assign values to add data can, but if the table header is not fixed, and the number is indeterminate, this needs to be based on the characteristics of the query data to add the export.
Export:
As shown, the number of items is indeterminate, the number of months of time is also uncertain, so simple through the template is not possible. And the information in the database is the information of each commodity at different time, so the data of the same time may have more than one, a commodity in different time distribution, so it can be more than one.
Code section:
[CSharp]View PlainCopy
- public int Datatabletoexcelbymproduct (DataTable dt_model, string sheetname)
- {
- Workbook = new Hssfworkbook ();
- Isheet sheet = workbook. Createsheet (SheetName);
- IRow row = null;
- Icell cell = null;
- //Style
- Icellstyle style = workbook. Createcellstyle ();
- Style. Alignment = HorizontalAlignment.Center; //Set the style of the cell: Horizontal Align Center
- Style. VerticalAlignment = Verticalalignment.center; //Set cell style: Vertical Align Center
- IFont font = workbook. CreateFont (); //Create a new Font style object
- Font. Boldweight = Short . MaxValue;
- Style. SetFont (font);
- DateTime nowtime = DateTime.Now;
- //number of product codes
- var dtgroup= (from P in Dt_model. AsEnumerable ()
- Group p by new {
- realname=p.field<string> ("Realname"),
- name=p.field<string> ("Name")
- }into g
- Select New {
- Realname=g.key.realname,
- Name=g.key.name,
- Counts=g.count ()
- }). ToList ();
- //Data table header, time
- row = sheet. CreateRow (0);
- Cell = row. Createcell (0);
- Cell. Setcellvalue ("time");
- Cell. CellStyle = style;
- //cellrangeaddress Four parameters: Start line, end row, start column, end column
- Sheet. Addmergedregion (new cellrangeaddress (0, 2, 0, 0));
- //Access port 1,1,2, number of ports
- Cell = row. Createcell (1);
- Cell. Setcellvalue ("Import and export commodity Code");
- Cell. CellStyle = style;
- //cellrangeaddress Four parameters: Start line, end row, start column, end column
- Sheet. Addmergedregion (new cellrangeaddress (0, 0, 1, 2*dtgroup. Count));
- //Product splicing
- row = sheet. CreateRow (1);
- row = sheet. CreateRow (2);
- For ( int c=0;c<dtgroup. count;c++)
- {
- //Create Import/Export bank cell, start
- Cell = row. Createcell (2*c+1);
- Cell. Setcellvalue (Dtgroup[c]. Realname);
- Cell. CellStyle = style;
- //cellrangeaddress Four parameters: Start line, end row, start column, end column
- Sheet. Addmergedregion (new Cellrangeaddress (1, 1, 2*c+1, 2*c+2));
- //Create imported cells
- //Create a column
- Cell=row. Createcell (2*c+1);
- Cell. Setcellvalue ("import");
- Cell=row. Createcell (2*c+2);
- Cell. Setcellvalue ("exit");
- }
- var allyearcount= (from P in Dt_model. AsEnumerable ()
- Group p by new {year_month=p.field<string> ("Year_month")} into M
- Select New
- {
- Yearmonth =m.key.year_month,
- Allyearcount=m.count ()
- }). ToList ();
- //year
- For (int i=0;i < allyearcount.count;i++)
- {
- Row=sheet. CreateRow (i+3);
- Cell=row. Createcell (0);
- string Yearmonth=allyearcount[i]. Yearmonth;
- int month =safeconvert.toint16 (yearmonth.substring (4,2));
- Cell. Setcellvalue ("1-" +month+"month");
- //Trade to touch situation
- For (int j=0;j<dtgroup. count;j++)
- {
- var items=dt_model. AsEnumerable (). Where (a=>a.field<string> ("Year_month") ==allyearcount[i]. Yearmonth && a.field<string> ("Name") ==dtgroup[j]. Name). ToList ();
- if (items. count>0)
- {
- Cell=row. Createcell (2*j+1);
- Cell. Setcellvalue (Items[0]. field<string> ("EXPORT_DESN"));
- Cell=row. Createcell (2*j+2);
- Cell. Setcellvalue (Items[0]. field<string> ("IMPORT_DESN"));
- }
- Else
- {
- Cell=row. Createcell (2*j+1);
- Cell. Setcellvalue ("");
- Cell=row. Createcell (2*j+2);
- Cell. Setcellvalue ("");
- }
- }
- }
- using (FileStream fsm=file.open (filename,filemode.openorcreate,fileaccess.readwrite))
- {
- Workbook. Write (FSM);
- Fsm. Close ();
- }
- return 1;
- }
In fact, the core part of the code is to create rows and columns and assign values to the table, if you have created a row is not created, as in this example, the time cell has created a row, so that "import and export commodity code" will not have to create the line again. However, creating a row must create a column that is created on top of the row, so even if the previous row has already created the column, the next row needs to be recreated.
Summary:
This method passes in the DataTable and the table name, if the data we return is not the direct output need to do some processing, we can use to add a field to the DataTable method, we want to store the results in the new field.
The idea of the export is the same, is to loop the rows and columns, in the table to assign values, the difference is from where to start assigning, the different places to solve, export is the same easy!
"C #" Excel exports merged rows and columns and dynamically loads rows and columns