初始化工作表:
public HttpResponseMessage ER_GenerateWBLWorksheet2()
{
ExcelPackage ExclPkg = new ExcelPackage();
ExcelWorksheet wsSheet1 = ExclPkg.Workbook.Worksheets.Add("Sheet1");
wsSheet1.Cells["A1"].Value = "ProductName";
wsSheet1.Cells["B1"].Value = "Price";
wsSheet1.Cells["C1"].Value = "ProductImageUrl";
wsSheet1.Cells["D1"].Value = "ProductUrl";
wsSheet1.Cells["E1"].Value = "StoreId";
wsSheet1.Cells["G1"].Value = "CategoryId";
wsSheet1.Cells["I1"].Value = "ColorId";
wsSheet1.Cells["K1"].Value = "FabricId";
wsSheet1.Cells["M1"].Value = "Gender";
创建类别列表:
var categorylist = wsSheet1.DataValidations.AddListValidation(wsSheet1.Cells["G2:G500"].Address);
var categories = _categoryServices.GetAllCategory();
var categoryEntities = (categories != null) ? categories.ToList() : new List<CategoryEntity>();
for (int i = 0; i < categoryEntities.Count; i++)
{
categorylist.Formula.Values.Add(categoryEntities[i].CategoryName.ToString());
}
categorylist.ShowErrorMessage = true;
categorylist.ErrorTitle = "Error";
categorylist.Error = "Please select the Category from Dropdown";
wsSheet1.Cells["G2:G500"].Style.Locked = false;
创建颜色列表:
var colorlist = wsSheet1.DataValidations.AddListValidation(wsSheet1.Cells["I2:I500"].Address);
var colors = _colorServices.GetAllColor();
var colorsEntities = (colors != null) ? colors.ToList() : new List<ColorEntity>();
for (int i = 0; i < colorsEntities.Count; i++)
{
colorlist.Formula.Values.Add(colorsEntities[i].ColorName.ToString());
}
colorlist.ShowErrorMessage = true;
colorlist.ErrorTitle = "Error";
colorlist.Error = "Please select the Color from Dropdown";
wsSheet1.Cells["I2:I500"].Style.Locked = false;
wsSheet1.Cells[wsSheet1.Dimension.Address].AutoFitColumns();
var filename = @"Inventory_" + DateTime.Now.ToString("M_dd_yyyy_H_M_s") + ".xlsx";
//string sPath = @"e:\" + filename;
var sPath = System.Web.Hosting.HostingEnvironment.MapPath("~/exportedfiles/" + filename);
Stream stream = File.Create(sPath);
ExclPkg.SaveAs(stream);
stream.Close();
string strServerPath = Convert.ToString(System.Configuration.ConfigurationManager.AppSettings["excelserverpath"]);
var sPathURL = (strServerPath + "exportedfiles/" + filename);
List<fileinfo> filelst = new List<fileinfo>();
fileinfo file = new fileinfo();
file.filename = filename;
file.fileurl = sPathURL;
//file.fileurl = sPath;
filelst.Add(file);
return new ResponseWrapper<fileinfo>(filelst, HttpStatusCode.OK, null, 0);
}
使用上面的代码我已经用下拉列表创建了excel,但我的要求是从下拉列表中选择多个值。是否可以使用epplus库?