Save SQL data to an Excel file.

Source: Internet
Author: User

Save SQL data to an Excel file.

Public string ExportExcel (DataSet ds, string saveFileName) {try {if (ds = null) return "database is empty"; bool fileSaved = false; Microsoft. office. interop. excel. application xlApp = new Microsoft. office. interop. excel. application (); if (xlApp = null) {return "an Excel object cannot be created. Your computer may not have Excel installed";} Microsoft. office. interop. excel. workbooks workbooks = xlApp. workbooks; Microsoft. office. interop. excel. workbook workboo K = workbooks. add (Microsoft. office. interop. excel. xlWBATemplate. xlWBATWorksheet); Microsoft. office. interop. excel. worksheet worksheet = (Microsoft. office. interop. excel. worksheet) workbook. worksheets [1]; // get sheet1 // write the field for (int I = 0; I <ds. tables [0]. columns. count; I ++) {worksheet. cells [1, I + 1] = ds. tables [0]. columns [I]. columnName;} // write value for (int r = 0; r <ds. tables [0]. rows. count; r ++) {(Int I = 0; I <ds. tables [0]. columns. count; I ++) {worksheet. cells [r + 2, I + 1] = ds. tables [0]. rows [r] [I];} System. windows. forms. application. doEvents ();} worksheet. columns. entireColumn. autoFit (); // adaptive column width. If (saveFileName! = "") {Try {workbook. saved = true; workbook. saveCopyAs (saveFileName); fileSaved = true;} catch (Exception ex) {fileSaved = false; MessageBox. show ("An error occurred while exporting the file. The file may be opened! \ N "+ ex. message) ;}} else {fileSaved = false;} xlApp. quit (); GC. collect (); // forcibly destroy if (fileSaved & System. IO. file. exists (saveFileName) System. diagnostics. process. start (saveFileName); // open EXCEL return "saved to Excel";} catch (Exception ex) {return ex. toString ();}}

 


C # Save the EXCEL document to the SQL database using windowsform programming. The EXCEL data format is shown in,

# Region insert Excel data into the database
/// <Summary>
/// Specified Excel file name
/// </Summary>
Private string strFileName;
// Whether the Excel file path has been obtained
Private bool hasFile;
// Whether the Excel file has been read to Datase
Private bool hasContent;
// Read content from Excel
Private DataSet dsExcel;
# Region converts Excel data to a DataTable
/// <Summary>
/// Convert Excel data to a able
/// </Summary>
Public void ShowExcelContent ()
{
// The following is an Excel database access operation:
// Connection string
String strCon = @ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = '" + strFileName + "'; Extended Properties = Excel 8.0 ";
// SQL statement to be executed
String strSql = "select * from [1 sequence paste $]";
// Create an OleDb connection object
OleDbConnection oleDbCon = new OleDbConnection (strCon );
// Create OleDbDataAdapter
OleDbDataAdapter oleDbDa = new OleDbDataAdapter (strSql, oleDbCon );
// Instantiate ds
DsExcel = new DataSet ();
// Open the connection
OleDbCon. Open ();
// Read the content from the database and fill it with ds
OleDbDa. Fill (dsExcel, "Info ");
// Close the connection
OleDbCon. Close ();

// Bind the data source
// BindingSource1.DataSource = d ...... remaining full text>

How can I save a separate table in the SQL database and open it in EXCEL?

1. First, find the table you want to export data.
2. Select save result as a file.
3. Select the output location. Select the output location [directly on the desktop]. You can't find the location without any help.
4. Now you can run the select * from student query statement on the student table in your query analyzer.
5. At this time, the SQL will automatically pop up the dialog box for saving the query results, and finally save it as rpt
6. There will be an rpt file. Right-click the file and select open mode. Select open in EXCEL]
7. opened successfully ~ You can save it as a common EXCEL file in EXCEL.
Finally, I hope the host will be successful! Hope to adopt it !!




Related Article

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.