"C #" Excel exports merged rows and columns and dynamically loads rows and columns

Source: Internet
Author: User

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
  1. public int Datatabletoexcelbymproduct (DataTable dt_model, string sheetname)
  2. {
  3. Workbook = new Hssfworkbook ();
  4. Isheet sheet = workbook. Createsheet (SheetName);
  5. IRow row = null;
  6. Icell cell = null;
  7. //Style
  8. Icellstyle style = workbook. Createcellstyle ();
  9. Style. Alignment = HorizontalAlignment.Center; //Set the style of the cell: Horizontal Align Center
  10. Style. VerticalAlignment = Verticalalignment.center; //Set cell style: Vertical Align Center
  11. IFont font = workbook. CreateFont (); //Create a new Font style object
  12. Font. Boldweight = Short .  MaxValue;
  13. Style. SetFont (font);
  14. DateTime nowtime = DateTime.Now;
  15. //number of product codes
  16. var dtgroup= (from P in Dt_model. AsEnumerable ()
  17. Group p by new {
  18. realname=p.field<string> ("Realname"),
  19. name=p.field<string> ("Name")
  20. }into g
  21. Select New {
  22. Realname=g.key.realname,
  23. Name=g.key.name,
  24. Counts=g.count ()
  25. }). ToList ();
  26. //Data table header, time
  27. row = sheet. CreateRow (0);
  28. Cell = row. Createcell (0);
  29. Cell.  Setcellvalue ("time");
  30. Cell. CellStyle = style;
  31. //cellrangeaddress Four parameters: Start line, end row, start column, end column
  32. Sheet.  Addmergedregion (new cellrangeaddress (0, 2, 0, 0));
  33. //Access port 1,1,2, number of ports
  34. Cell = row. Createcell (1);
  35. Cell. Setcellvalue ("Import and export commodity Code");
  36. Cell. CellStyle = style;
  37. //cellrangeaddress Four parameters: Start line, end row, start column, end column
  38. Sheet. Addmergedregion (new cellrangeaddress (0, 0, 1, 2*dtgroup.  Count));
  39. //Product splicing
  40. row = sheet. CreateRow (1);
  41. row = sheet. CreateRow (2);
  42. For ( int c=0;c<dtgroup. count;c++)
  43. {
  44. //Create Import/Export bank cell, start
  45. Cell = row. Createcell (2*c+1);
  46. Cell. Setcellvalue (Dtgroup[c]. Realname);
  47. Cell. CellStyle = style;
  48. //cellrangeaddress Four parameters: Start line, end row, start column, end column
  49. Sheet.  Addmergedregion (new Cellrangeaddress (1, 1, 2*c+1, 2*c+2));
  50. //Create imported cells
  51. //Create a column
  52. Cell=row. Createcell (2*c+1);
  53. Cell.  Setcellvalue ("import");
  54. Cell=row. Createcell (2*c+2);
  55. Cell.      Setcellvalue ("exit");
  56. }
  57. var allyearcount= (from P in Dt_model. AsEnumerable ()
  58. Group p by new {year_month=p.field<string> ("Year_month")} into M
  59. Select New
  60. {
  61. Yearmonth =m.key.year_month,
  62. Allyearcount=m.count ()
  63. }). ToList ();
  64. //year
  65. For (int i=0;i < allyearcount.count;i++)
  66. {
  67. Row=sheet. CreateRow (i+3);
  68. Cell=row. Createcell (0);
  69. string Yearmonth=allyearcount[i].  Yearmonth;
  70. int month =safeconvert.toint16 (yearmonth.substring (4,2));
  71. Cell.  Setcellvalue ("1-" +month+"month");
  72. //Trade to touch situation
  73. For (int j=0;j<dtgroup. count;j++)
  74. {
  75. var items=dt_model. AsEnumerable (). Where (a=>a.field<string> ("Year_month") ==allyearcount[i]. Yearmonth && a.field<string> ("Name") ==dtgroup[j]. Name).  ToList ();
  76. if (items. count>0)
  77. {
  78. Cell=row. Createcell (2*j+1);
  79. Cell. Setcellvalue (Items[0].  field<string> ("EXPORT_DESN"));
  80. Cell=row. Createcell (2*j+2);
  81. Cell. Setcellvalue (Items[0].  field<string> ("IMPORT_DESN"));
  82. }
  83. Else
  84. {
  85. Cell=row. Createcell (2*j+1);
  86. Cell.  Setcellvalue ("");
  87. Cell=row. Createcell (2*j+2);
  88. Cell.  Setcellvalue ("");
  89. }
  90. }
  91. }
  92. using (FileStream fsm=file.open (filename,filemode.openorcreate,fileaccess.readwrite))
  93. {
  94. Workbook. Write (FSM);
  95. Fsm. Close ();
  96. }
  97. return 1;
  98. }



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

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.