使用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 ...
随机推荐
- How to fix updating ubuntu apt-get problem
It's my new PC with a new os of ubuntu. every time when I want to install software or update apt-get ...
- Atitit Immutability 和final的优点
Atitit Immutability 和final的优点 什么是 immutability? 其实细分起来有语法上的 immutable (例如 Java 里的 final 关键字), 和运行时对象 ...
- Atitit usrQBF2312 命名空间pkg 以及 api命名 spec规范
Atitit usrQBF2312 命名空间pkg 以及 api命名 spec规范 简化英文1 常用类库sdk的命名单词统计表1 简化时间规则1 Namsspace nam spec,参照java . ...
- Atitit (Sketch Filter)素描滤镜的实现 图像处理 attilax总结
Atitit (Sketch Filter)素描滤镜的实现 图像处理 attilax总结 1.1. 素描滤镜的实现方法比较简单,这里我们直接写出算法过程如下:1 1.2. 颜色减淡COLOR_DO ...
- salesforce 零基础学习(二十七)VF页面等待(loading)效果制作
进行查询的情况下,显示友好的等待效果可以让用户更好的了解目前的状态以及减少用户消极的等待,例如下图所示. VF提供了<apex:actionStatus>标签,,此标签用于显示一个AJAX ...
- css绘制三角形原理
1.新建一个元素,将它的宽高都设置为0:然后通过设置border属性来实现三角形效果,下面是css绘制三角形的原理: <!DOCTYPE html> <html> <he ...
- 关于Thread.currentThread()和this的差异
重新来看多线程时,被这结果搞懵逼了.不多说,直接上代码: public class MyThread02 extends Thread { public MyThread02() { System.o ...
- 【WP 8.1开发】推送通知测试服务端程序
所谓推送通知,用老爷爷都能听懂的话说,就是: 1.我的服务器将通知内容发送到微软的通知服务器,再由通知服务器帮我转发消息. 2.那么,微软的推送服务器是如何知道我的服务器要发消息给哪台手机呢?手机客户 ...
- Tcl Tk Introduction
Tcl Tk Introduction eryar@163.com 摘要Abstract:Tcl/Tck脚本可以很容易实现用户自定义的命令,方便的创建图形化的用户界面GUI,所以Tcl和Tk的应用领域 ...
- ab压力测试
原文链接:http://www.orlion.ga/698/ ab是个什么就不说了搞lamp的都会知道.主要看一下结果都是什么意义. ab 的用法是:ab [options] [http://]hos ...