Hi everyone,
i found a way to slove my question. Just use one xls file to make a templete. And then comment the source code (in Libraries\Nop.BusinessLogic\ExportImport\ExportManager.cs) where make a table. Just below method:
public static void ExportProductsToXls(string filePath, ProductCollection products)
{
using (ExcelHelper excelHelper = new ExcelHelper(filePath))
{
excelHelper.Hdr = "YES";
excelHelper.Imex = "0";
Dictionary<string, string> tableDefinition = new Dictionary<string, string>();
int maxStringLength = 200;
//tableDefinition.Add("ID", "int");
//tableDefinition.Add("Name", string.Format("nvarchar({0})", maxStringLength));
//tableDefinition.Add("ShortDescription", string.Format("nvarchar({0})", maxStringLength));
//tableDefinition.Add("FullDescription", string.Format("nvarchar({0})", maxStringLength));
//tableDefinition.Add("ProductTypeId", "int");
//tableDefinition.Add("TemplateId", "int");
//tableDefinition.Add("ShowOnHomePage", "nvarchar(5)");
//tableDefinition.Add("MetaKeywords", string.Format("nvarchar({0})", maxStringLength));
//tableDefinition.Add("MetaDescription", string.Format("nvarchar({0})", maxStringLength));
//tableDefinition.Add("MetaTitle", string.Format("nvarchar({0})", maxStringLength));
//tableDefinition.Add("AllowCustomerReviews", "nvarchar(5)");
//tableDefinition.Add("AllowCustomerRatings", "nvarchar(5)");
//tableDefinition.Add("Published", "nvarchar(5)");
//tableDefinition.Add("SKU", string.Format("nvarchar(200)", maxStringLength));
//tableDefinition.Add("ManufacturerPartNumber", string.Format("nvarchar({0})", maxStringLength));
//tableDefinition.Add("IsGiftCard", "nvarchar(5)");
//tableDefinition.Add("IsDownload", "nvarchar(5)");
//tableDefinition.Add("DownloadId", "int");
//tableDefinition.Add("UnlimitedDownloads", "nvarchar(5)");
//tableDefinition.Add("MaxNumberOfDownloads", "int");
//tableDefinition.Add("DownloadActivationType", "int");
//tableDefinition.Add("HasSampleDownload", "nvarchar(5)");
//tableDefinition.Add("SampleDownloadId", "int");
//tableDefinition.Add("HasUserAgreement", "nvarchar(5)");
//tableDefinition.Add("UserAgreementText", String.Format("nvarchar({0})", maxStringLength));
//tableDefinition.Add("IsRecurring", "nvarchar(5)");
//tableDefinition.Add("CycleLength", "int");
//tableDefinition.Add("CyclePeriod", "int");
//tableDefinition.Add("TotalCycles", "int");
//tableDefinition.Add("IsShipEnabled", "nvarchar(5)");
//tableDefinition.Add("IsFreeShipping", "nvarchar(5)");
//tableDefinition.Add("AdditionalShippingCharge", "decimal");
//tableDefinition.Add("IsTaxExempt", "nvarchar(5)");
//tableDefinition.Add("TaxCategoryId", "int");
//tableDefinition.Add("ManageInventory", "int");
//tableDefinition.Add("StockQuantity", "int");
//tableDefinition.Add("DisplayStockAvailability", "nvarchar(5)");
//tableDefinition.Add("MinStockQuantity", "int");
//tableDefinition.Add("LowStockActivityId", "int");
//tableDefinition.Add("ProductCode", string.Format("nvarchar({0})", maxStringLength));
//tableDefinition.Add("AllowOutOfStockOrders", "nvarchar(5)");
//tableDefinition.Add("OrderMinimumQuantity", "int");
//tableDefinition.Add("OrderMaximumQuantity", "int");
//tableDefinition.Add("DisableBuyButton", "nvarchar(5)");
//tableDefinition.Add("Price", "decimal");
//tableDefinition.Add("OldPrice", "decimal");
//tableDefinition.Add("ProductCost", "decimal");
//tableDefinition.Add("CustomerEntersPrice", "nvarchar(5)");
//tableDefinition.Add("MinimumCustomerEnteredPrice", "decimal");
//tableDefinition.Add("MaximumCustomerEnteredPrice", "decimal");
//tableDefinition.Add("Weight", "decimal");
//tableDefinition.Add("Length", "decimal");
//tableDefinition.Add("Width", "decimal");
//tableDefinition.Add("Height", "decimal");
//tableDefinition.Add("CreatedOn", "decimal");
//excelHelper.WriteTable("Products", tableDefinition);
...............................................Other source just the same
}
}
Because i have made a templete just contain the Products table. And i also change the width for all column and make the xls templete file to auto newline.So now the content will fix width as i want.
Because all the export file have only one , use time name to seprate. And i copy templete xls file and give only name for the export file just like before(just like :products_2011-02-13-00-00-07_4290.xls) which the source code in NopCommerceStore\Administration\Modules\Products.ascx.cs
protected void btnExportXLS_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
try
{
string fileName = string.Format("products_{0}_{1}.xls", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss"), CommonHelper.GenerateRandomDigitCode(4));
string templateName = "products.xls";
/*first you should create a new template file in NopCommerceStore\files\ExportImport and name products.xls
products.xls file just the table [products] info. And you can fix the width for that as you want*/
*/
string filePath = string.Format("{0}files\\ExportImport\\{1}", HttpContext.Current.Request.PhysicalApplicationPath, fileName);
string templeteFilePath = string.Format("{0}files\\ExportImport\\{1}", HttpContext.Current.Request.PhysicalApplicationPath, templateName);
//Here should let the templete file use for everytime you want to export, so need to copy a new xls file to insert data
File.Copy(templeteFilePath, filePath, true);
ProductCollection products = GetProducts(0);
ExportManager.ExportProductsToXls(filePath, products);
CommonHelper.WriteResponseXls(filePath, fileName);
}
catch (Exception exc)
{
ProcessException(exc);
}
}
}
It can really work , just can create a new excel in NopCommerceStore\files\ExportImport, and the width just as the templete. But here have another question, i cannot use browser to open that. and also download. When i select "Open" or "download" ,it just let me download products.aspx page. I don't why.
Any thoughs help me?
Thanks!