简体   繁体   中英

Moving data from Sql Server to Excel via a Web Service

My users want data from my application in Excel. The data resides in a SQL Server database but I don't want the users to have direct access to the database, I would rather provide them a web service to get the data. What is the best way to move data from SQL Server to Excel via a web service?

You can do it as a straight asp.net page and return a .csv file. If you change the mimetype to text/csv, it should default to open in excel. This would be the easiest approach, and one that I've used in the past with great success.

The following code will generate an excel file from a datatable, you can just stream this to the user

    public static void CreateExcelFromDataTable(string filename, DataTable dt) {

        DataGrid grid = new DataGrid();
        grid.HeaderStyle.Font.Bold = true;
        grid.DataSource = dt;
        grid.DataMember = dt.TableName;

        grid.DataBind();

        // render the DataGrid control to a file

        using (StreamWriter sw = new StreamWriter(filename)) {
            using (HtmlTextWriter hw = new HtmlTextWriter(sw)) {
                grid.RenderControl(hw);
            }
        }
    }

你也可以使用.xls文件名回滚一个带有表格的html页面,excel知道如何打开它

Have the Web service emit a string buffer where cells are delimited by the tab character and rows by a carriage return/line feed. Then pipe the results of this into Excel with an Excel Web query. It's cheap, quick, a little bit dirty, but good for simple processes.

I'm in favor of Excel Web Query (Data/Import External) Also, there is a way to post data from Excel back to the web site. At least it's working in MS Sharepoint

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM