对不起,我的英语!
我的DB有两个具有测试结果的表:
dbo.Result

第二表提供了有关学校的信息:
dbo.School

这样的记录大概在40.000到70.000之间。最后,我需要得到这样的报告文件(pdf):

我的解决方案:

LearnerReport.cs
namespace so16092016.Models
{
public class LearnerReport
{
public string SNS { get; set; } //Surname Name SecondName
public string SchoolName { get; set; }
public string ClassName { get; set; }
public int TestResult5 { get; set; }
}
}Program.cs
using Excel = Microsoft.Office.Interop.Excel;
namespace so16092016
{
class Program
{
static void Main(string[] args)
{
resultsEntities context = new resultsEntities();
ResultsRepository resultsRepository = new ResultsRepository(context);
var ma_results = resultsRepository.GetTList().Where(x => x.SubjectCode == 2); //получить результаты по математике
Excel.Application app = new Excel.Application();
app.DisplayAlerts = false;
Excel.Workbook book_template = app.Workbooks.Open(@"шаблон_отчета.xlsx");
Excel._Worksheet sheet_template = book_template.Sheets["отчет"];
foreach(var ob in ma_results)
{
//1. Создаем объкт LearnerReport из БД
LearnerReport report = new LearnerReport
{
SNS = $"{ob.surname} {ob.name} {ob.SecondName}",
SchoolName = ob.SchoolName,
ClassName = ob.ClassName,
TestResult5 = ob.TestResult5
};
//2. Экспорт объкта LearnerReport в шаблон xlsx
sheet_template.Range["C4"].Value2 = report.SNS;
sheet_template.Range["C5"].Value2 = report.SchoolName;
sheet_template.Range["C6"].Value2 = report.ClassName;
sheet_template.Range["C9"].Value2 = report.TestResult5;
//3. Сохраняем полученный файл в .pdf на рабочем столе
string file_name = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\{report.SNS}.pdf";
sheet_template.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, file_name);
}
book_template.Close(0);
book_template = null;
app.Quit();
app = null;
}
}
}我需要的:应用程序工作良好,并给我正确的结果-报告。但是正如您所看到的,代码对于OOP/SOLID并不好。因此,很难扩大规模。请帮助我/显示正确的模式以删除此任务:
发布于 2016-09-16 18:58:45
一个可能的改进是将导出逻辑提取到单独的服务:
static void Main(string[] args)
{
resultsEntities context = new resultsEntities();
ResultsRepository resultsRepository = new ResultsRepository(context);
var ma_results = resultsRepository.GetTList().Where(x => x.SubjectCode == 2); //получить результаты по математике
IReportService reportService = new ExcelReportService();
reportService.GenerateReport(ma_results);
}
public interface IReportService
{
void GenerateReport(IEnumerable<StudentDto> students);
}
public class ExcelReportService:IReportService
{
public void GenerateReport(IEnumerable<StudentDto> students)
{
Excel.Application app = new Excel.Application();
app.DisplayAlerts = false;
Excel.Workbook book_template = app.Workbooks.Open(@"шаблон_отчета.xlsx");
Excel._Worksheet sheet_template = book_template.Sheets["отчет"];
foreach (var ob in students)
{
//1. Создаем объкт LearnerReport из БД
LearnerReport report = new LearnerReport
{
SNS = $"{ob.surname} {ob.name} {ob.SecondName}",
SchoolName = ob.SchoolName,
ClassName = ob.ClassName,
TestResult5 = ob.TestResult5
};
//2. Экспорт объкта LearnerReport в шаблон xlsx
sheet_template.Range["C4"].Value2 = report.SNS;
sheet_template.Range["C5"].Value2 = report.SchoolName;
sheet_template.Range["C6"].Value2 = report.ClassName;
sheet_template.Range["C9"].Value2 = report.TestResult5;
//3. Сохраняем полученный файл в .pdf на рабочем столе
string file_name = $@"{Environment.GetFolderPath(Environment.SpecialFolder.Desktop)}\{report.SNS}.pdf";
sheet_template.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, file_name);
}
book_template.Close(0);
book_template = null;
app.Quit();
app = null;
}
}发布于 2016-09-16 19:28:25
我认为将Excel模板保存在没有Excel的格式中会更容易一些,比如
XML Spreadsheet 2003 (*.xml)Single File Web Page (*.mht,*.mhtml)Web Page (*.htm,*.html)您可以在模板(如{report.SNS} )中使用占位符,并在XML/HTML或Excel中用以下内容替换它们:
for(;;)
{
var cell = sheet_template.UsedRange.Find("{*}", Type.Missing, XlFindLookIn.xlValues,
XlLookAt.xlWhole, XlSearchOrder.xlByRows, XlSearchDirection.xlNext);
if(cell == null) break;
var value = cell.Value2 as string;
switch (value)
{
case "{report.SNS}": cell.Value2 = report.SNS; break;
// case "{report.SchoolName}": .. etc.
default: // log issue
}
}https://stackoverflow.com/questions/39534322
复制相似问题