我们正在开发Web api,可从中下载excel和数据。在网上搜索时,我们发现了npoi,epplus,closedxml之类的库。
我们真的需要使用这些库来与excel一起使用还是采用标准方法?
我们正在使用asp.net核心进行Web api开发。
编辑:基本上,我们的前端位于我们所处的角度5 正在公开网络API。在Web api中,我们编写了逻辑来获取数据,并且在获取数据后,我们需要以提供的特定格式/模板(单元格,列方式,表格方式等)放置数据。我们需要在excel中导出很多行。
我们的数据库和API都是基于天蓝色的。
对此有任何帮助!
答案 0 :(得分:2)
我使用过epplus,我认为它在这种情况下效果很好。让我给你举个例子。 导出数据
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="Def" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="/LOGS/Acb/d.log"/>
<param name="MaxFileSize" value="10000KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<logger name="com.xyz.rest">
<logger value="fatal"/>
<appender-ref ref="Def"/>
</logger>
</log4j:configuration>
可以使用MVC将返回的ExcelPackage对象作为下载文件发送到文件中
private ExcelPackage CreateDoc(string title, string subject, string keyword)
{
var p = new ExcelPackage();
p.Workbook.Properties.Title = title;
p.Workbook.Properties.Author = "Application Name";
p.Workbook.Properties.Subject = subject;
p.Workbook.Properties.Keywords = keyword;
return p;
}
public ExcelPackage getApplicantsStatistics()
{
ExcelPackage p = CreateDoc("Applicant Statistics", "Applicant statistics", "All Applicants");
var worksheet = p.Workbook.Worksheets.Add("Applicant Statistics");
//Add Report Header
worksheet.Cells[1, 1].Value = "Applicant Statistics";
worksheet.Cells[1, 1, 1, 3].Merge = true;
//Get the data you want to send to the excel file
var appProg = _unitOfWork.ApplicantsProgram
.AllIncluding(pr => pr.Program1)
.GroupBy(ap => ap.Program1.Name)
.Select(ap => new { programName = ap.Key, TotalNum = ap.Count() })
.ToList();
//First add the headers
worksheet.Cells[2, 1].Value = "SR No";
worksheet.Cells[2, 2].Value = "Program";
worksheet.Cells[2, 3].Value = "No. of Applicants";
//Add values
var numberformat = "#,##0";
var dataCellStyleName = "TableNumber";
var numStyle = p.Workbook.Styles.CreateNamedStyle(dataCellStyleName);
numStyle.Style.Numberformat.Format = numberformat;
for (int i = 0; i < appProg.Count; i++)
{
worksheet.Cells[i + 3, 1].Value = i + 1;
worksheet.Cells[i + 3, 2].Value = appProg[i].programName;
worksheet.Cells[i + 3, 3].Value = appProg[i].TotalNum;
}
// Add to table / Add summary row
var rowEnd = appProg.Count + 2;
var tbl = worksheet.Tables.Add(new ExcelAddressBase(fromRow: 2, fromCol: 1, toRow: rowEnd, toColumn: 3), "Applicants");
tbl.ShowHeader = true;
tbl.TableStyle = TableStyles.Dark9;
tbl.ShowTotal = true;
tbl.Columns[2].DataCellStyleName = dataCellStyleName;
tbl.Columns[2].TotalsRowFunction = RowFunctions.Sum;
worksheet.Cells[rowEnd, 3].Style.Numberformat.Format = numberformat;
// AutoFitColumns
worksheet.Cells[2, 1, rowEnd, 3].AutoFitColumns();
return p;
}
答案 1 :(得分:1)
有几个不错的库,我最喜欢的是 Microsoft提供的EPPlus和OpenXML
https://github.com/JanKallman/EPPlus
https://docs.microsoft.com/en-us/office/open-xml/open-xml-sdk
您的数据库和前端没有什么区别,因为一切都是由后端组织的。