【发布时间】:2018-06-07 19:05:24
【问题描述】:
如何使用 Aspose total 或 Aspose Cells 从数据库中填充 excel 表 提供一个 Excel 模板,其中可能包含在填写 Excel 文档后应保持活动状态的公式。
【问题讨论】:
标签: excel templates using fill aspose
如何使用 Aspose total 或 Aspose Cells 从数据库中填充 excel 表 提供一个 Excel 模板,其中可能包含在填写 Excel 文档后应保持活动状态的公式。
【问题讨论】:
标签: excel templates using fill aspose
好吧,要将数据从您的数据源导入或合并到 Excel 文件,我们有两种选择,您可以根据需要尝试任何一种。 例如
1) 使用 Aspose.Cells 提供的智能标记功能。因此,您可以创建一个设计器模板文件,将标记插入工作表中的单元格,您也可以根据需要对单元格进行相应的格式化。例如,您可以为与不同数据集或根据您想要的记录等相关的数据库表创建报告。智能标记是根据您想要的数据集/结果集进行处理的,这可能是查询或存储过程的结果,因此您可以使用您自己的代码和 ADO.NET API 来指定或编写要处理的查询,并获取要填充到 DataTable 或变量/数组中的数据。处理标记后,会将数据插入单元格中以代替设计器文件工作表中粘贴的标记,请参阅document 以获取完整参考。
2) 使用 Aspose.Cells 提供的不同数据源的数据导入选项。例如,您可以使用 Cells.ImportDataTable() 方法导入数据表以填充工作簿中的工作表。请参阅文档以获取完整参考。
附言。我在 Aspose 担任支持开发人员/宣传员。
【讨论】:
在您的项目中创建一个文件夹后,您将拥有要生成的 Excel 文件并将 Aspose.Total 添加到您的使用状态中。创建如下方法生成excel 文件:
protected void CurrentExcel_Click(object sender, EventArgs e){
//getting the items that will fill the cells(should be different
//than below)
Items searchItems = new SearchItems();
searchItems.ProjectStatusIDs = new List<int> { 24721 };
List<CurrentRecord> resultsRecords =
YourEntity.GetCurrentRecords().OrderBy(c => c.LOCATION).ToList();
// the template Excel file that you will fill
string fileName = "currents_list_Excel_Shortened.xlsx";
//define a workbook that will help you access Excel file cells
Workbook wb = new Workbook(Server.MapPath(string.Format(@"
{0}/{1}", "~/Templates/", fileName)));
//adding worksheet that will be filled
wb.Worksheets.Add(SheetType.Worksheet);
Worksheet ws = wb.Worksheets[0];
ws.Name = "Current Shortened";
try
{
Aspose.Cells.Cells wsCells = ws.Cells;
int x = 8;
foreach (CurrentRecord mwa in resultsRecords)
{
Cell Cell1 = ws.Cells[x, 0];
Cell Cell2 = ws.Cells[x, 1];
Cell Cell3 = ws.Cells[x, 2];
Cell Cell4 = ws.Cells[x, 3];
Cell Cell5 = ws.Cells[x, 4];
Cell Cell6 = ws.Cells[x, 5];
Cell Cell7 = ws.Cells[x, 6];
Cell Cell8 = ws.Cells[x, 7];
Cell Cell9 = ws.Cells[x, 8];
Cell Cell10 = ws.Cells[x, 9];
Cell Cell11 = ws.Cells[x, 10];
Cell Cell12 = ws.Cells[x, 11];
Cell Cell13 = ws.Cells[x, 12];
Cell Cell14 = ws.Cells[x, 13];
// here filling your object properties to the cells which
//should be different than the one below
Cell1.PutValue(mwa.ID + "-" +
mwa.LOCATION);
Cell2.PutValue(mwa.number);
Cell3.PutValue(mwa.Rate + " " + mwa.POSTMILE + " " +
mwa.POSTMILE_KPList);
Cell4.PutValue(mwa.PROJECT_LOCATION_TYPE);
Cell5.PutValue(mwa.RELName.Split(' ')[0] + "/" +
mwa.RECell);
if (mwa.COMPANY_NAME != "")
{
Cell6.PutValue(mwa.COMPANY_NAME.IndexOf('-') != -1 ?
mwa.COMPANY_NAME.Split(' ')[0] :
mwa.COMPANY_NAME.Split(' ')[0] + ' ' +
mwa.COMPANY_NAME.Split(' ')[1]);
}
Cell7.PutValue(mwa.PROJECT_STATUS);
Cell8.PutValue(mwa.PROJECT_LOCATION_WORKING_DAYS);
Cell9.PutValue(mwa.PROJECT_STATUS_PE_DAYS);
Cell10.PutValue(mwa.PROJECT_STATUS_WORK_SUSPENDED == true
? "Yes" : "NO");
Cell11.PutValue(string.Format("{0:0.######}",
mwa.PROJECT_STATUS_WORK_COMPLETED) + "/" +
string.Format("{0:0.######}",
mwa.PROJECT_STATUS_TIME_COMPLETED));
Cell12.PutValue(mwa.M600 != null ? string.Format("{0:d}",
mwa.M600) : "TBD");
Cell13.PutValue(mwa.Contractual != null ? string.Format("
{0:d}", mwa.Contractual) : "TBD");
Cell14.PutValue(mwa.PROJECT_STATUS_UPDATED_EST_COMPLETION
!= null ? string.Format("{0:d}",
mwa.PROJECT_STATUS_UPDATED_EST_COMPLETION) : "TBD");
x++;
}
wb.Save(HttpContext.Current.Response, fileName,
Aspose.Cells.ContentDisposition.Attachment, new
XlsSaveOptions(Aspose.Cells.SaveFormat.Xlsx));
}
catch(Exception ex)
{
throw;
}
}
【讨论】: