Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C # to realize Excel Export of NPOI

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use C # to achieve Excel export of NPOI", the content of the explanation is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "how to use C # to achieve Excel export of NPOI" bar!

Technical points:

1. Customize the attribute property

two。 Taking the attribute attribute value of a class and its attributes by reflection

3.NPOI package commonly used properties and methods (I only know what has been used, strange ones are either known by name or checked by Baidu)

Realize the function points:

Template export for List class objects. For practical scenario examples, please see the last code block (emm...) It's still more abstract, see code.

EXCEL export class DTO superclass

Defines the property description class of the inheritance export class DTO

Excel help class

There are actually a lot of points to talk about in this section, and the key is that the data sources used for EXCEL export are strongly typed.

You can see that list is actually a collection of classes after Queryable toList () of EF, which exists as a data source.

The DTO DesWeeklyReportExcExp in it inherits ExcelSuper, and attributes are added to classes and properties, respectively.

Public class XXXXController: CoreController {/ / Controller Internal [HttpPost] public ActionResult export () {/ / Controller Interface var list = op .GetPagedQuery (PageModel) .Select (s = > new DesWeeklyReportExcExp {col1 = s.Project.ProjName Col2 = s.ColAttROPDate1?.ToString ("yyyy.MM.dd"), col3 = (s.ColAttROPDate2 = = null? None: s.ColAttROPDate2.Value.ToString ("yyyy.MM.dd") + "/" + (s.ColAttROPDate3 = = null? "none": s.ColAttROPDate3.Value.ToString ("yyyy.MM.dd")), col4 = s.ColAttROPDate4?.ToString ("yyyy.MM.dd")}) .ToList (); string filePath = Server.MapPath ("~ / download/ [this is the template name] .xlsx"); string filename = Path.GetFileNameWithoutExtension (filePath) / / File name string extension = Path.GetExtension (filePath); / / suffix name with dot (.) String fileDownloadName = filename + extension; var fs = ExcelHelper.ExportToExcel (list, filePath). ToArray (); return File (fs, "application/ms-excel", fileDownloadName);}} [ExcelExpClassAttribute (2,0,2,0)] public class DesWeeklyReportExcExp: ExcelSuper {/ column 1 / [ExcelExp (SortIndex = 0, ColName = "column 1")] public string col1 {get; set } / column 2 / [ExcelExp (SortIndex = 0, ColName = "column 2")] public string col2 {get; set;} / column 3 / [ExcelExp (SortIndex = 0, ColName = "column 3")] public string col3 {get; set } / column 4 / / [ExcelExp (SortIndex = 0, ColName = "column 4")] public string col4 {get; set }} Thank you for your reading, the above is the content of "how to use C # to achieve Excel export of NPOI". After the study of this article, I believe you have a deeper understanding of how to use C # to achieve Excel export of NPOI, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report