How to build a flexible, configurable exported data to Excel solutions.

xiaoxiao2021-03-18  229

Tell me, do you often encounter data formats to export such

Describe what we have to do:

1. Support multi-mesh definition.

2. Fixed a fixed value in a fixed

3. There is a data area

4. One list may insert different backgrounds, pictures, and note according to some data conditions.

Even if there is a format that requires this

Do we have to write each time you use a code, and can you only modify the configuration file?

This is no problem, as long as you

Understand my ideas and

Play your own imagination ability can be expanded.

Yes, expand, this is what I want to emphasize.

Many of the articles of the articles cannot help you expand, but only let you go to copy, this is not what we want.

Let us speed up the speed to meet the XML files based on the above requirements.

Abcd describes our Excel to use this way, which of which of which SHEET is, which is output.

It has established data to export to Excel correspondence.

CSSClass is description style class

DBTOEXCELRELATION is a description of the total data correspondence class (it may contain multiple Sheet correspondence)

FixEdItem is a fixed grid

RelationItem is the data area corresponding class (it is a recursable class)

SheetMapRelation This is an Excelsheet class (which contains FixedItem, RELATIONITEM, CSSCLASS)

As shown below:

Let's take a look at our export interface

1

Using system;

2

Using system.data;

3

4

Using Excelexporter.configs;

5

USING EXCEL;

6

Seduce

8

Namespace Excelexporter.exporter

9

{

10

Public Delegate Void OnDataBindHandler (Excel.Range Excelrange, DataRow Row, String Columnname);

11

///

<

Summary

>

12

/// iConvertor's summary description.

13

///

Summary

>

14

Public interface ipporter

15

{

16

In one

String Execute (System.Data.DataSet DS, DBTOEXCELRELATION DBEXCELREALTION, STRING FILEPATH);

18

19

20

Event ondatabindhandler overdatabind;

twenty one

twenty two

twenty three

}

twenty four

}

25

Note that we have declared an event so that we can handle special problems.

Such as these questions

One column may insert different backgrounds, pictures, and note according to some data conditions. Below is a part of the code to create Excel, here is completed, create a fixed grid, create a header, create a data area.

1

protected

Virtual

Void

CreateExcel ()

2

{3 4 5 TRY 6 {7 App = New Excel.ApplicationClass (); 8} 9 Catch (System.Exception EX) 10 {11 throw new exception ("Unapled EXCEL object; Details:


" EX.MESSAGE); 12} 13 app.displayalerts = false; 1415 // //app.userControl = true; 16 wbs = app.workbooks; 17 int nowSheet = 0; 18 try19 {20 21 / / wbc = wbs.Open (_filepath, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt); 22 wbc = wbs.Add (_objOpt); 23 for (int sheetIndex = 0; sheetIndex

40 allrange.Font.Name = _dbrelation.DataCss.FontName; 41 allrange.Font.Size = _dbrelation.DataCss.FontSize;. 42 43} 4445 4647 firstRange = ws.get_Range (sheetmapR.DataStartRange.Trim () ToUpper (), this ._Objopt); 48 // Cancel all selected 49 first.select (); 505152 / ** //// created fixed grid 53 createfixedRange (SheetMapr.fixedItems); 54 / ** //// Create Head 55 CreateTitle sheetmapR.Items, sheetmapR.MaxDepth, firstRange, 0); 5657 Excel.Range datastartrange = firstRange.get_Offset (sheetmapR.MaxDepth, _objOpt);!! 58 if (_ds = null && _ ds.Tables [sheetIndex] = null) 59 CreateDataRange (datastartrange , SheetMapr, _ds.tables [SheetIndex]); / ** //// creation data area 60} 61} 6263 _fileName = System.DateTime.Now.ToString ( "yy_MM_dd-hh_mm_ss_ff") "xls."; 64 String tempName = _filepath "/" _fileName; 65 wbc.SaveAs (tempName, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt, Excel.XlSaveAsAccessMode.xlExclusive, _objOpt, _objOpt, _objOpt, _objOpt, _objOpt); 6667 68} 69 catch (System.Exception ex) 70 {71 throw new Exception ( "Unable to create Excel file correctly;
details:


"Create" NowSheet.toString () "Sheet:" EX.MESSAGE); 72} 73 Finally74 {75 this.dispose ();

76} 77} The creation of multi-headed head is a difficult point, I have already talked about it in blog,

This article says the foundation: two things, width, depth of multi-headed head.

Http://www.cnblogs.com/king_astar/archive/2005/05/11/153071.html

Finally, call it,

DBTOEXCELRELATION DBT

=

New

DBTOEXCELRELATION (); DBT

=

DBT.DSERIALIZE (Server.mappath

"

ExcelexPotertest.xml

"

))); BaseExporter B

=

New

BaseExporter (); String FileName

=

B.Execute (DT.DataSet, DBT, Server.mappath (

"

./excels

"

));

转载请注明原文地址:https://www.9cbs.com/read-129962.html

New Post(0)