如何使用Aspose从数据库中填写excel表格
如何使用Aspose total或Aspose单元格从数据库中填写excel工作表如何使用Aspose从数据库中填写excel表格,excel,templates,using,fill,aspose,Excel,Templates,Using,Fill,Aspose,如何使用Aspose total或Aspose单元格从数据库中填写excel工作表 提供可能包含公式的Excel模板,这些公式在填写Excel文档后应保持活动状态。好的,要将数据从数据源导入或合并到Excel文件,我们有两个选项,您可以根据需要尝试任何一个。 e、 g 1) 使用Aspose.Cells提供的智能标记功能。因此,您可以创建一个设计器模板文件,将标记插入到图纸中的单元格中,也可以根据需要相应地格式化单元格。例如,您可以为与不同数据集相关的数据库表或根据所需记录等创建报告。智能标记基
提供可能包含公式的Excel模板,这些公式在填写Excel文档后应保持活动状态。好的,要将数据从数据源导入或合并到Excel文件,我们有两个选项,您可以根据需要尝试任何一个。 e、 g 1) 使用Aspose.Cells提供的智能标记功能。因此,您可以创建一个设计器模板文件,将标记插入到图纸中的单元格中,也可以根据需要相应地格式化单元格。例如,您可以为与不同数据集相关的数据库表或根据所需记录等创建报告。智能标记基于所需的数据集/结果集(可能是查询或存储过程的结果)进行处理,因此,您可以使用自己的代码(例如ADO.NET API)指定或编写要处理的查询,并将数据填充到DataTable或variables/Array中。处理标记后,数据将插入到单元格中,以代替设计器文件图纸中粘贴的标记,有关完整的参考,请参见 2) 使用Aspose.Cells提供的不同数据源的数据导入选项。例如,您可以使用Cells.ImportDataTable()方法导入数据表以填充工作簿中的工作表。请参阅该文件以获取完整的参考资料
另外,我在Aspose担任支持开发人员/福音传道者。在项目中创建了一个文件夹,其中包含要生成的Excel文件,并将Aspose.Total添加到using Stations中。创建以下方法以生成excel 文件:
protectedvoid CurrentExcel\u单击(对象发送者,事件参数e){
//获取将填充单元格的项目(应不同
//(低于下表)
Items searchItems=新的searchItems();
searchItems.ProjectStatusIDs=新列表{24721};
列表结果记录=
YourEntity.GetCurrentRecords().OrderBy(c=>c.LOCATION.ToList();
//将填充的模板Excel文件
string fileName=“currents\u list\u Excel\u shorted.xlsx”;
//定义可帮助您访问Excel文件单元格的工作簿
工作簿wb=新工作簿(Server.MapPath(string.Format)(@)
{0}/{1}',“~/Templates/”,文件名));
//添加将要填写的工作表
wb.Worksheets.Add(SheetType.Worksheet);
工作表ws=wb.工作表[0];
ws.Name=“当前缩短”;
尝试
{
Aspose.Cells.Cells wsCells=ws.Cells;
int x=8;
foreach(结果记录中的CurrentRecord mwa)
{
Cell-Cell1=ws.Cells[x,0];
Cell-Cell2=ws.Cells[x,1];
Cell-Cell3=ws.Cells[x,2];
Cell4=ws.Cells[x,3];
Cell5=ws.Cells[x,4];
Cell6=ws.Cells[x,5];
Cell7=ws.Cells[x,6];
Cell8=ws.Cells[x,7];
Cell9=ws.Cells[x,8];
Cell10=ws.Cells[x,9];
Cell-Cell11=ws.Cells[x,10];
Cell12=ws.Cells[x,11];
Cell13=ws.Cells[x,12];
Cell14=ws.Cells[x,13];
//在这里,将对象属性填充到
//应该与下面的不同
单元格1.PutValue(mwa.ID+“-”+
mwa.位置);
单元格2.PutValue(mwa.编号);
单元格3.PutValue(mwa.Rate+“”+mwa.POSTMILE+“”+
mwa.POSTMILE_KPList);
单元格4.PutValue(mwa.项目位置类型);
Cell5.PutValue(mwa.RELName.Split(“”)[0]+“/”+
mwa.RECell);
如果(mwa.COMPANY_NAME!=“”)
{
单元格6.PutValue(mwa.COMPANY_NAME.IndexOf('-')!=-1?
mwa.公司名称拆分(“”)[0]:
mwa.公司名称.拆分(“”)[0]+'+
mwa.公司名称拆分(“”)[1]);
}
单元格7.PutValue(mwa.项目状态);
第8单元PutValue(mwa、项目地点、工作日);
第9单元PutValue(mwa、项目状态和天数);
Cell10.PutValue(mwa.PROJECT\u STATUS\u WORK\u SUSPENDED==真
?“是”:“否”);
Cell11.PutValue(string.Format(“{0:0.########}”),
mwa.项目状态工作完成)+“/”+
string.Format(“{0:0.\\\\\\\\\\\\\\\\\\\}”,
mwa.项目状态(完成时间);
Cell12.PutValue(mwa.M600!=null?string.Format(“{0:d}”),
mwa.M600):“待定”;
Cell13.PutValue(mwa.contractive!=null?string.Format(“
{0:d},mwa.合同):“待定”;
单元格14.PutValue(mwa.PROJECT\u状态\u更新\u EST\u完成
!=null?string.Format(“{0:d}”,
mwa.项目状态更新(预计完工):“待定”);
x++;
}
wb.Save(HttpContext.Current.Response,文件名,
Aspose.Cells.ContentDisposition.Attachment,新建
XlsSaveOptions(Aspose.Cells.SaveFormat.Xlsx));
}
捕获(例外情况除外)
{
投
}
}
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;
}
}