This article is a mirror article of machine translation, please click here to jump to the original article.

View: 210205|Reply: 67

[WebAPI] webapi exports list data to an Excel table file

  [Copy link]
Posted on 7/16/2018 10:26:35 PM | | | |
To export Excel tables using C#, you need to use NPOI, which is introduced as follows:

NPOI refers to a program built on top of POI version 3.x, which can read and write Word or Excel documents without Office installed.
NPOI is an open-source C# project for reading and writing Microsoft OLE2 component documents such as Excel and WORD.

A lot of code on the Internet needs to be stored on the server, turned into a physical file, and then exported (or given a download link).The code of this post does not need to save a physical file, and the list data can be exported and downloaded directly

Let's take a look at the renderings first:


We use a browser and access the :http://localhost:63096/api/download/test interface address to download the excel file.

Once the download is complete, we open the test.xls and it can be opened and read normally! As shown below:



Prepare list data, code:



The webapi code is as follows:



The EntityListToExcel2003 method is as follows:





Previous:Asp.net MVC Bundle packages all CSS files together
Next:vs2017 browser input path to terminate debugging
Posted on 1/7/2019 11:22:53 AM |
Very good, I use this generic
/// <summary>
        Export a set of objects to EXCEL
        /// </summary>
        <typeparam name="T"> the type of object you want to export</typeparam>
        <param name="objList" > a set of objects</param>
        <param name="FileName"> the exported file name</param>
        <param name="columnInfo"> column name information</param>
        public void ListToExcel<T>(List<T> objList, string FileName, Dictionary<string, string> columnInfo)
        {
            if (columnInfo.Count == 0) { return; }
            if (objList.Count == 0) { return; }
            Generate EXCEL HTML
            string excelStr = "";
            Type myType = objList[0]. GetType();
            Based on the reflection, the attribute to be displayed is obtained from the attribute name information passed in
            List<System.Reflection.PropertyInfo> myPro = new List<System.Reflection.PropertyInfo>();
            foreach (string cName in columnInfo.Keys)
            {
                System.Reflection.PropertyInfo p = myType.GetProperty(cName);
                if (p != null)
                {
                    myPro.Add(p);
                    excelStr += columnInfo[cName] + "\t";
                }
            }
            If no available attributes are found, it ends
            if (myPro.Count == 0) { return; }
            excelStr += "\n";
            foreach (T obj in objList)
            {
                foreach (System.Reflection.PropertyInfo p in myPro)
                {
                    excelStr += p.GetValue(obj, null) + "\t";
                }
                excelStr += "\n";
            }
            Output EXCEL
            HttpResponse rs = System.Web.HttpContext.Current.Response;
            rs. ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            rs. AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
            rs. ContentType = "application/ms-excel";
            rs. Write(excelStr);
            rs. End();
        }
 Landlord| Posted on 7/31/2019 1:22:31 PM |
chenxingen168 Posted on 2019-5-13 22:12
Good code, thanks for sharing

You are welcome
Posted on 6/18/2019 2:39:34 PM |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Posted on 7/20/2018 8:09:57 AM |
Mountains and rivers

Score

Number of participants1MB-1 contribute-1 Collapse reason
QWERTYU -1 -1 Do not pour water, improving the quality of reply posts is the duty of every member.

See all ratings

Posted on 7/25/2018 9:23:54 AM |
You can take a look, it's better
Posted on 7/25/2018 9:27:42 AM |
It's pretty good
Posted on 8/22/2018 3:56:01 PM |
EntityListToExcel2003 method content
Posted on 9/5/2018 4:24:07 PM |
Water water water

Score

Number of participants1MB-1 contribute-1 Collapse reason
QWERTYU -1 -1 Do not pour water, improving the quality of reply posts is the duty of every member.

See all ratings

Posted on 9/6/2018 9:30:18 AM |
Like one, mark it, and use it in the future!
Posted on 9/19/2018 3:05:17 PM |
Very useful, thanks
Posted on 9/27/2018 8:56:58 AM |
Support the owner to update~
Posted on 9/27/2018 9:14:28 AM |
Find out
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com