Aspose下载图片

/// <summary>
/// 把DataTable数据按照Excel模板导出到Excel
/// </summary>
/// <param name="templateFileName">模板名称</param>
/// <param name="fileName">Excel文件路径</param>
/// <param name="ds">DataSet</param>
/// <returns></returns>
public static BatchDownloadResponse DataTableToExcel(string templateFileName, string fileName, DataSet ds)
{
if (!string.IsNullOrEmpty(templateFileName)) templateFileName = templateFileName.Replace("\\\\", "\\");
if (!string.IsNullOrEmpty(fileName)) fileName = fileName.Replace("\\\\", "\\");
BatchDownloadResponse response = new BatchDownloadResponse(fileName);
if (ds == null || ds.Tables.Count == 0)
{
response.AddError(0, "Data is empty, check the file!");
return response;
}
string licPath = System.Configuration.ConfigurationManager.AppSettings["AsposeLicPath"];
License lic = new License();
if (!string.IsNullOrEmpty(licPath)) lic.SetLicense(licPath);
WorkbookDesigner wd = new WorkbookDesigner();

wd=new WorkbookDesigner(new Workbook(templateFileName));
wd.SetDataSource(ds);
//wd.SetDataSource("GEO", "GEO");
//wd.SetDataSource("Product Group", "Product Group");
//wd.SetDataSource("Product Series", "Product Series");
//wd.SetDataSource("Product Family", "Product Family");
//wd.Workbook.Worksheets[0].Cells.DeleteColumn(1,true);

wd.Process();
wd.Workbook.CalculateFormula();
wd.Workbook.Save(fileName, SaveFormat.Xlsx);

wd = null;

FileInfo fi = new FileInfo(fileName);
response.FileName = fi.Name;
return response;
}

 

 

switch (productGroup)
{
case "NOTEBOOK":
productGroup= "NB";
break;

}

List<byte> result = new List<byte>();
string filePath = string.Format(@"{0}\Documents\TempFolder\PriceCable_Download_{1}.xlsx", HttpContext.Current.Request.PhysicalApplicationPath, DateTime.Now.ToString("yyyyMMddhhmmss"));
AsposeHelper.DataTableToExcel(templateFileName, filePath, ds);
Workbook workbook = new Workbook(filePath);
Worksheet sheet = workbook.Worksheets[0];
sheet.Name = productGroup.Replace("/", "");// + "Price cable";
int picIndex = sheet.Pictures.Add(0, 1, 3, 3, imagefilePath);
Aspose.Cells.Drawing.Picture pic = sheet.Pictures[picIndex];
pic.HeightCM = 1.3;
int linkIndex = sheet.Hyperlinks.Add("D7", 1, 1, "'Terms & Conditions'!A1");
Hyperlink hyperLink = sheet.Hyperlinks[linkIndex];
hyperLink.ScreenTip = "'Terms & Conditions'!A1";
hyperLink.TextToDisplay = "Pls refer to \"Terms & Conditions\" tab (click here)";

if (sheetName.Trim() != null && sheetName != "")
{
workbook.Worksheets[sheetName].IsVisible = true;
workbook.Worksheets[sheetName].Name = "MOQ";
sheetName = "MOQ";
int linkIndex2 = sheet.Hyperlinks.Add("D8", 1, 1, "\'" + sheetName + "\'!A1");
Hyperlink hyperLink2 = sheet.Hyperlinks[linkIndex2];
hyperLink2.ScreenTip = "'" + sheetName + "'!A1";
hyperLink2.TextToDisplay = "Pls refer to \"MOQ\" tab (click here)";
}
else {
//sheet.AutoFitRows(7, 8);
int linkIndex2 = sheet.Hyperlinks.Add("B8", 1, 1, "");
Hyperlink hyperLink2 = sheet.Hyperlinks[linkIndex2];
hyperLink2.TextToDisplay = "";
}

workbook.Worksheets.ActiveSheetIndex = 0;

Cells cells = sheet.Cells;
DataTable dt = ds.Tables[1];
int index = 14;
int[] centerCollection = new int[] { 1, 2, 3, 11, 12,13,14,15 };
int[] rightCollection = new int[] { 5, 6, 7, 8, 9, 10 };
CellValueFormatStrategy format = new CellValueFormatStrategy();
string split = @"
";
string descriptionValue = "";
string[] descriptionLines;
foreach (DataRow dr in dt.Rows)
{
if (dr["IsSeries"].ToString() == "1")
{
for (int i = 1; i < 16; i++)
{
cells[index, i].SetStyle(GetCellStyle(workbook, "bgSeries"));
}
}
else if (dr["IsFamily"].ToString() == "1")
{
for (int i = 1; i < 16; i++)
{
cells[index, i].SetStyle(GetCellStyle(workbook, "bgFamily"));
}
}
else
{
for (int i = 1; i < 16; i++)
{
string styleTemp = "";
switch (dr["ItemTypeHidden"].ToString())
{

case "0":
styleTemp = "bgText";
break;
case "1":
styleTemp = "bgBundle";
break;
case "2":
styleTemp = "bgOption";
break;
}
if (centerCollection.Contains(i))
{
if (i == 5 || i == 7 || i == 9 || i == 10)
{
cells[index, i].SetStyle(GetCellStyle(workbook, styleTemp + "CenterInteger"));
}
else
{
cells[index, i].SetStyle(GetCellStyle(workbook, styleTemp + "Center"));
}
}
else if (rightCollection.Contains(i))
{
if (i == 6 || i == 8)//这两列是百分比显示
{
styleTemp = styleTemp + "CenterPercent";
}
else if (i == 5 || i == 7 || i == 9 || i == 10)
{
styleTemp = styleTemp + "CenterInteger";
}
else
{
styleTemp = styleTemp + "CenterInteger";
}
cells[index, i].SetStyle(GetCellStyle(workbook, styleTemp));
}
else if (i == 13)
{
cells[index, i].SetStyle(GetCellStyle(workbook, styleTemp + "CenterRed"));
}
else if (i == 4)
{
cells[index, i].SetStyle(GetCellStyle(workbook, styleTemp + "Left"));//先设置Description列左对齐
//设置自动行高
AutoFitterOptions option = new AutoFitterOptions() { AutoFitMergedCells = false, OnlyAuto = true, IgnoreHidden = false };
sheet.AutoFitRows(index, index + 1, option);

descriptionValue = cells[index, i].GetStringValue(format);//获取Description列的值
if (descriptionValue.Length < 1)
{
continue;//如果Description为空,就结束此次循环
}
descriptionLines = descriptionValue.Split(new string[] { split }, StringSplitOptions.None);
if (descriptionLines.Length > 1)
{
//如果Description为多行,就自定义设置行高,将会覆盖刚才的自动行高
double lineHight = descriptionLines.Length * 15;
cells.SetRowHeight(index, lineHight);
}
}
}
}

index++;
}
if(CountryCode.ToUpper()!= "SINGAPORE")
{
cells.HideColumn(14);
}
workbook.Save(filePath);
return filePath;

posted @ 2018-12-07 16:46  奔跑的熊猫  阅读(284)  评论(0编辑  收藏  举报