使用Aspose插件对Excel操作
使用使用Aspose插件对Excel文档进行导入导出操作
使用前请先下载Aspose插件引用
Excel导入:
前台使用file标签获取,submit方式提交。
<form id="form1" enctype="multipart/form-data" method="post" >
<table class="table-condensed">
<tr>
<td class="text-right">导入表格:</td>
<td class="text-left"><input type="file" name="file1" class="btn btn-default btn-lg" /></td>
</tr>
<tr>
<td class="text-left">
<input type="submit" id="btnImport" name="btnImport" value="导入" class="btn btn-default" />
</td>
</tr>
</table> </form>
后台接收:
HttpPostedFileBase fileBase = Request.Files["file1"];//这里获取名称与前台标签name保持一致
if (fileBase != null)
{
string filename = Path.GetFileName(fileBase.FileName);
string extension = Path.GetExtension(filename);
string path = "/Upload/Test/" + DateTime.Now.ToString("yyyyMMdd") + "/";
Directory.CreateDirectory(Path.GetDirectoryName(Request.MapPath(path)));
string newFilename = DateTime.Now.ToString("yyyyMMddHHmmssfff");
string fullFileName = path + newFilename + extension;
fileBase.SaveAs(Request.MapPath(fullFileName));
try
{
Stopwatch sw = new Stopwatch();//记录导入操作用时多长
sw.Start();
//这里可放入BLL方法处理
string result = new ProductBLL().ImportExcel(Request.MapPath(path), newFilename, extension); //BLL方法 ProductBLL
public string ImportExcel(string path, string filename, string extension)
{
Workbook workbook = new Workbook(path + filename + extension);
Worksheet worksheet = workbook.Worksheets[0];
Cells cells = worksheet.Cells;
for (int i = 1; i < cells.Rows.Count; i++)
{
try
{
string brand = cells[i, 0].StringValue.Trim();//获取列值
string years = cells[i, 1].StringValue.Trim();
}
catch (Exception e)
{
continue;
} }
return "OK";
} sw.Stop();
long runTime = sw.ElapsedMilliseconds / ; //获取到操作用时多少秒
}
catch (Exception e)
{
Log.Write("导入", "导入错误", "错误信息:" + e.Message);
} } Excel导出:
string path = "/Upload/Test/" + DateTime.Now.ToString("yyyyMMdd") + "/";
Directory.CreateDirectory(Path.GetDirectoryName(Server.MapPath(path)));
string newFilename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls";
string fullFileName = Server.MapPath(path + newFilename); public void ExportInfo(List<Test> list, string fullFileName)
{
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[]; cellSheet.PageSetup.LeftMargin = 0.3;//左边距
cellSheet.PageSetup.RightMargin = 0.3;//右边距
cellSheet.PageSetup.TopMargin = ;//上边距
cellSheet.PageSetup.BottomMargin = 0.5;//下边距
cellSheet.PageSetup.FooterMargin = 0.5;//页脚
cellSheet.PageSetup.HeaderMargin = 0.5;//页眉
cellSheet.PageSetup.Orientation = PageOrientationType.Landscape;
cellSheet.PageSetup.CenterHorizontally = true;//水平居中
cellSheet.PageSetup.CenterVertically = true; cellSheet.Cells[, ].PutValue("货号");
cellSheet.Cells[, ].PutValue("颜色");
cellSheet.Cells[, ].PutValue("尺码"); int i = ;
foreach (var item in list)
{
cellSheet.Cells[i, ].PutValue(item.productno);
cellSheet.Cells[i, ].PutValue(item.size);
cellSheet.Cells[i, ].PutValue(item.color);
i++;
}
cellSheet.AutoFitColumns(); fullFileName = Path.GetFullPath(fullFileName);
workbook.Save(fullFileName);
} return File(fullFileName, "application/ms-excel", UserName + "_Test单" + newFilename);// 方法Action里直接返回File文件下载。
//DataTable数据源导出,封装ToExcel方法。
da.ToExcel(fullFileName);
return File(fullFileName, "application/ms-excel", UserName + "_Test单" + newFilename);
/// <summary>
/// DataTable数据表保存至Excel
/// </summary>
/// <param name="dt">数据源</param>
/// <param name="fullFileName">文件完整路径</param>
public static void ToExcel(this System.Data.DataTable dt, string fullFileName)
{
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0];
cellSheet.Name = dt.TableName;
int rowIndex = 0;
int colIndex = 0;
int colCount = dt.Columns.Count;
int rowCount = dt.Rows.Count;
//列名的处理
for (int i = 0; i < colCount; i++)
{
cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);
//cellSheet.Cells[rowIndex, colIndex].SetStyle.Font.IsBold = true;
//cellSheet.Cells[rowIndex, colIndex].Style.Font.Name = "宋体";
colIndex++;
}
Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
style.Font.Name = "Arial";
style.Font.Size = 10;
Aspose.Cells.StyleFlag styleFlag = new Aspose.Cells.StyleFlag();
cellSheet.Cells.ApplyStyle(style, styleFlag);
rowIndex++;
for (int i = 0; i < rowCount; i++)
{
colIndex = 0;
for (int j = 0; j < colCount; j++)
{
cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].ToString());
colIndex++;
}
rowIndex++;
}
cellSheet.AutoFitColumns();
fullFileName = Path.GetFullPath(fullFileName);
workbook.Save(fullFileName);
}
由于抽取部分代码出来,排版和引用方面未做详细注释,前台和后台方法对应即可。
做个笔记日后方便自己查看使用。做个笔记日后方便自己查看使用。做个笔记日后方便自己查看使用。做个笔记日后方便自己查看使用。
使用Aspose插件对Excel操作的更多相关文章
- C# Aspose.Cells.dll Excel操作总结
简介 Aspose.Cells是一款功能强大的 Excel 文档处理和转换控件,不依赖 Microsoft Excel 环境,支持所有 Excel 格式类型的操作. 下载 Aspose.Cells.d ...
- 报表中的Excel操作之Aspose.Cells(Excel模板)
原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...
- 使用Aspose.Cells读取Excel
最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...
- aspose.Cells 导出Excel
aspose aspse.Cells可以操作Excel,且不依赖于系统环境. 使用模板,通过绑定输出数据源 这种适合于对格式没有特别要求的,直接绑定数据源即可.和数据绑定控件差不多. Workbook ...
- python笔记8 - excel操作
前提: python操作excel需要使用的模块有xlrd.xlwt.xlutils.对excel进行读.写.更新操作.操作excel时需要先导入这些模块,demo如下: excel-读操作知识点: ...
- Python+Excel 操作对比
前言 从网页爬下来的大量数据需要excel清洗成堆的科学实验数据需要导入excel进行分析作为一名面向逼格的Python程序员该如何合理而又优雅的选择生产力工具呢? 得益于辛勤劳作的python大神们 ...
- Aspose.Cells导出Excel(1)
利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...
- Npoi导入导出Excel操作
之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...
- Delphi Excel 操作大全
Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...
随机推荐
- .NET学习笔记 -- 那堆名词到底是啥(CLR、CLI、CTS、CLS、IL、JIT)
什么是CLR? CLR,公共语言运行时(Common Language Runtime)是一个由多种语言使用的“运行时”.他的核心功能包括(内存管理.程序集加载.安全性.异常处理和线程同步),可以被面 ...
- easy-ui JOB 及 小记录
$:获取 $.ajax({ type: "POST" , url: "" , cont ...
- IOS 手势-轻点、触摸、手势、事件
1.概念 手势是从你用一个或多个手指接触屏幕时开始,直到手指离开屏幕为止所发生的所有事件.无论手势持续多长时间,只要一个或多个手指仍在屏幕上,这个手势就存在. 触摸是指把手指放到IOS设备的屏幕上,从 ...
- [C#反射]C#中的反射解析及使用.
1.对C#反射机制的理解2.概念理解后,必须找到方法去完成,给出管理的主要语法3.最终给出实用的例子,反射出来dll中的方法 参考: C#反射,MSDN编程指南 反射是一个程序集发现及运行的过程,通过 ...
- 1.1.1. Atitit Cocos2d-JS v3.x的问题
1.1.1. Atitit Cocos2d-JS v3.x的问题 1.1. Api 兼容性 场景切换的api都不兼容了... 删除了比较多的api...可以dep啊.. 1.2. gui布局,, ...
- Java-集合(没做出来)第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列。 例如: List list = new ArrayList(); list.add(“Hello”); list.add(“World”); list.add(“Learn”); //此时list 为Hello World Learn reverseL
没做出来 第四题 (List)写一个函数reverseList,该函数能够接受一个List,然后把该List 倒序排列. 例如: List list = new ArrayList(); list.a ...
- KendoUI系列:PanelBar
<link href="@Url.Content("~/Content/kendo/2014.1.318/kendo.common.min.css")" ...
- WPF自定义控件与样式(8)-ComboBox与自定义多选控件MultComboBox
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 下拉选 ...
- 深入理解CSS中的长度单位
前面的话 本文分为绝对长度单位和相对长度单位来介绍CSS中的长度单位的主要知识 绝对长度单位 绝对长度单位代表一个物理测量 像素px(pixels) 在web上,像素px是典型的度量单位,很多其他长度 ...
- Android线程机制——AsyncTask
对于Android为什么要使用多线程,因为从Android4.0之后,谷歌规定了网络操作不允许放在主线程中执行,由此就有了多线程的机制,有个JAVA学习经验的朋友一定知道多线程指的是什么,简单来讲就是 ...