/// <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;

Aspose下载图片的更多相关文章

  1. C++根据图片url下载图片

    需要使用到URLDownloadToFile()函数,该函数在头文件<urlmon.h>中声明. URLDownloadToFile()函数的定义如下: HRESULT URLDownlo ...

  2. .net 已知图片的网络路径,通过浏览器下载图片

    没什么技术含量,主要留给自己查找方便: 如题,知道图片的完整网络路径的情况下,在浏览器中下载图片的实现: 下面这个方法实现的是把图片读取为byte数组: private byte[] GetImage ...

  3. (TODO:)下载图片,报错:warning: could not load any Objective-C class information from the dyld shared cache. This will significantly reduce the quality of type information available.

    想使用NSInvocationOperation下载图片,然而并没有下载下来, NSData为nil, 还有报错:(打断点就报错) warning: could not load any Object ...

  4. Android 本地/网路下载图片实现放大缩小

     Android 本地加载/网路下载图片实现放大缩小拖拉效果,自定义控件. package com.example.ImageViewCustom; import android.app.Activi ...

  5. 利用node来下载图片到本地

      本文是针对于知道图片地址的下载图片方法. 同时也是我的处男作(额,怪怪的〜);不要在意这些细节. 最近在弄项目迁移,需要把http的链接全换成https的:以前的cms不支持http的协议,然后就 ...

  6. SDWebImage下载图片有时候无法成功显示出来

    之前用下面的方法现在图片,有时候会出现图片没有下载成功显示: - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)p ...

  7. 根据url地址单个或批量下载图片

    我们在java开发的时候会遇到通过url地址下载图片的情况.方便起见,我把通过url地址下载图片封装了tool工具类,方便以后使用 1.根据如:http://abc.com/hotels/a.jpg  ...

  8. 使用HttpURLConnection下载图片

    import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.Ht ...

  9. Android下载图片/调用系统相机拍照、显示并保存到本地

    package com.example.testhttpget; import java.io.BufferedReader; import java.io.FileNotFoundException ...

随机推荐

  1. 循环使用 v-for 指令。

    循环语句 循环使用 v-for 指令. v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名. v-for 可以绑定数据到 ...

  2. [xDebug] 服务器端的配置参数

    [Xdebug] ;load xdebug extensionzend_extension_ts = path/tp/xdebug;是否开启自动跟踪xdebug.auto_trace = On;是否开 ...

  3. Mariadb 10.14 mysqldump error: 1049

    Mariadb 10.14 mysqldump error: 1049 Table of Contents 1. 错误信息 2. 解决方法 1 错误信息 执行表导出: mysqldump -uroot ...

  4. Java并发编程的艺术(一、二章) ——学习笔记

    第一章  并发编程的挑战 需要了解的一些概念 转自 https://blog.csdn.net/TzBugs/article/details/80921351 (1) 同步VS异步 同步和异步通常用来 ...

  5. belongsTo和hasOne的区别

    简单的讲就是,没有太大的区别,只是在逻辑上出现的思想的偏差(逻辑的合理性).belongsTo:可以理解为属于hasOne:可以理解为拥有 首先,我们创建两张表.user表 字段 id name pa ...

  6. Java并发编程(05):悲观锁和乐观锁机制

    本文源码:GitHub·点这里 || GitEE·点这里 一.资源和加锁 1.场景描述 多线程并发访问同一个资源问题,假如线程A获取变量之后修改变量值,线程C在此时也获取变量值并且修改,两个线程同时并 ...

  7. Python学习日志-02

    (2)Python如何运行程序 Python解释器简介: Python不仅仅是一门编程语言,它也是一个名为解释器的软件包.解释器是一种让其他程序运行起来的程序.当你编写了一段Python程序,Pyth ...

  8. GIT本地库基本操作-命令行

    GIT本地库操作基本原理 GIT作为分布式版本库软件,每个机器上都是一个版本库. git初始化后,有三个区,分别是 工作区,暂存区,本地库: 工作区是我们编辑代码的区别,包括新增,修改,删除代码操作, ...

  9. Area.js下载

    因为vant AddressEdit 地址编辑的必要组件area.js网站经常进不去,所以存在这里,area.js 代码如下: export default { province_list: { 11 ...

  10. 01MySQL内核分析-The Skeleton of the Server Code

    摘要 这个官方文档一段对MySQL内核分析的一个向导.是对MySQL一条insert语句写入到MySQL数据库的分析. 但是,对于MySQL 5.7版本来说,基本上都是写入到innodb引擎.但也还是 ...