Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to export tables to EXCEL in C#winform
How to export tables to EXCEL in C#winform
The data in the dataset are converted into Excel files.

///& lt; Summary & gt

///The summary description of the exported file.

///function: convert the data in DataSet into Excel file.

///Description: Export an Excel file.

///Remarks: Please refer to the COM component corresponding to Office. The method of exporting an Excel object needs to call some of its methods and properties.

///& lt; /summary & gt;

Common class export file

{

Private string filePath =

Public export file (string excel_path)

{

//

// TODO: Add constructor logic here.

//

filePath = excel _ path

}

///& lt; Summary & gt

///Export the specified data set to an Excel file.

///& lt; /summary & gt;

///& lt; param name = " dt " & gt& lt/param & gt;

///& lt; returns & gt& lt/returns & gt;

public bool ExportToExcel(System。 Data set ds, string report name)

{

If (ds. Table [0]. Row. Count == 0)

{

MessageBox。 Show ("data set is empty");

}

Microsoft . office . interop . excel . _ Application XL app = new Application class();

Workbook xlbook = xlapp. workbooks . Add(true);

Worksheet xlsheet = (worksheet) xlbook Worksheet [1];

Range Range = XL sheet . get _ Range(XL app。 Cells [1, 1], xlapp. Cell [1, ds. tables[0]. columns . count]);

Range. MergeCells = true

xlapp。 active cell . formula 1c 1 = report name;

xlapp。 ActiveCell . Font.Size = 20

xlapp。 ActiveCell . Font.Bold = true

xlapp。 active cell . horizontal alignment = Microsoft。 office . interop . excel . constants . XL center;

int colIndex = 0;

int RowIndex = 2;

//Start writing the title of each column.

Foreach (DataColumn dc in ds. Table [0]. Column)

{

colindex++;

xlsheet。 Cell [RowIndex, colIndex] = dc. Title;

}

//Start writing.

int RowCount = ds。 tables[0]. rows . count; //number of rows

for(int I = 0; I & ltRowCounti++)

{

rowindex++;

int ColCount = ds。 Table [0]. columns. count; //Number of columns

for(colIndex = 1; colIndex & lt= ColCountcolIndex++)

{

xlsheet。 Cell [RowIndex, colIndex] = ds. Table [0]. Line [i] [colindex-1]; //dg[i,colIndex- 1];

xlsheet。 Cell. Column width = ds. Table [0]. Line [i][colIndex-1]. ToString()。 Length;

}

}

xlbook。 Saved = true

xlbook。 SaveCopyAs(file path);

xlapp。 quit();

GC。 collect();

Return true

}

public bool ExportToExcelOF(System。 Data set ds, string report name)

{

If (ds. Table [0]. Row. Count == 0)

{

MessageBox。 Show ("data set is empty");

}

string FileName = filePath

//system. DataTable dt = new system. data . DataTable();

FileStream objFileStream

StreamWriter objStreamWriter

string strLine =

ObjFileStream = new file stream (file name, file mode. OpenOrCreate,FileAccess。 Write);

objStreamWriter = new StreamWriter(objFileStream,System。 text . encoding . unicode);

strLine = ReportName

objStreamWriter。 WriteLine(strLine);

strLine =

for(int I = 0; Me & ltds. Table [0]. columns. count; i++)

{

strLine = strLine + ds。 Table [0]. columns[I]. column name . tostring()+" "+Convert。 Tochar (9);

}

objStreamWriter。 WriteLine(strLine);

strLine =

for(int I = 0; Me & ltds. tables[0]. rows . count; i++)

{

strLine = strLine+(I+ 1)+Convert。 Tochar (9);

for(int j = 1; j & ltds。 Table [0]. columns. count; j++)

{

strLine = strLine + ds。 Table [0]. Line [i][j] ToString() + Convert。 Tochar (9);

}

objStreamWriter。 WriteLine(strLine);

strLine =

}

objStreamWriter。 close();

objFileStream。 close();

//Microsoft . office . interop . excel . _ Application XL app = new Application class();

//workbook xlbook = xlapp. workbooks . Add(true);

//worksheet xlsheet = (worksheet) xlbook. Worksheet [1];

//Range Range = XL sheet . get _ Range(XL app。 Cells [1, 1], xlapp. Cell [1, ds. tables[0]. columns . count]);

//range. entire column . AutoFit();

//xlapp。 quit();

Return true

}

}