使用使用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操作的更多相关文章

  1. C# Aspose.Cells.dll Excel操作总结

    简介 Aspose.Cells是一款功能强大的 Excel 文档处理和转换控件,不依赖 Microsoft Excel 环境,支持所有 Excel 格式类型的操作. 下载 Aspose.Cells.d ...

  2. 报表中的Excel操作之Aspose.Cells(Excel模板)

    原文:报表中的Excel操作之Aspose.Cells(Excel模板) 本篇中将简单记录下Aspose.Cells这个强大的Excel操作组件.这个组件的强大之处,就不多说,对于我们的报表总是会有导 ...

  3. 使用Aspose.Cells读取Excel

      最新更新请访问: http://denghejun.github.io Aspose.Cells读取Excel非常方便,以下是一个简单的实现读取和导出Excel的操作类: 以下是Aspose.Ce ...

  4. aspose.Cells 导出Excel

    aspose aspse.Cells可以操作Excel,且不依赖于系统环境. 使用模板,通过绑定输出数据源 这种适合于对格式没有特别要求的,直接绑定数据源即可.和数据绑定控件差不多. Workbook ...

  5. python笔记8 - excel操作

    前提: python操作excel需要使用的模块有xlrd.xlwt.xlutils.对excel进行读.写.更新操作.操作excel时需要先导入这些模块,demo如下: excel-读操作知识点: ...

  6. Python+Excel 操作对比

    前言 从网页爬下来的大量数据需要excel清洗成堆的科学实验数据需要导入excel进行分析作为一名面向逼格的Python程序员该如何合理而又优雅的选择生产力工具呢? 得益于辛勤劳作的python大神们 ...

  7. Aspose.Cells导出Excel(1)

    利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public ...

  8. Npoi导入导出Excel操作

    之前公司的一个物流商系统需要实现对订单的批量导入和导出,翻阅了一些资料,最后考虑使用NPOI实现这个需求. 在winform上面实现excel操作:http://www.cnblogs.com/Cal ...

  9. Delphi Excel 操作大全

    Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...

随机推荐

  1. Redis学习笔记~关于空间换时间的查询案例

    回到目录 空间与时间 空间换时间是在数据库中经常出现的术语,简单说就是把查询需要的条件进行索引的存储,然后查询时为O(1)的时间复杂度来快速获取数据,从而达到了使用空间存储来换快速的时间响应!对于re ...

  2. jQuery实现全选、全不选、反选

    如图,需要使用jQuery实现全选.全不选.反选功能: 核心代码: 全选 $("#check_all").click(function(){ $("input:check ...

  3. jQuery插件开发的五种形态[转]

    这篇文章主要介绍了jQuery插件开发的五种形态小结,具体的内容就是解决javascript插件的8种特征,非常的详细. 关于jQuery插件的开发自己也做了少许研究,自己也写过多个插件,在自己的团队 ...

  4. [CSS]复选框单选框与文字对齐问题的研究与解决.

    前言:今天碰到的这个问题, 恰好找到一个很好的博文, 在这里转载过来 学习下. 原文地址:复选框单选框与文字对齐问题的研究与解决. 目前中文网站上面的文字,就我的个人感觉而言,绝大多数网站的主流文字大 ...

  5. 25.按要求编写一个Java应用程序: (1)编写一个矩形类Rect,包含: 两个属性:矩形的宽width;矩形的高height。 两个构造方法: 1.一个带有两个参数的构造方法,用于将width和height属性初化; 2.一个不带参数的构造方法,将矩形初始化为宽和高都为10。 两个方法: 求矩形面积的方法area() 求矩形周长的方法perimeter() (2)通过继承Rect类编写一个具有

    package zhongqiuzuoye; //自己写的方法 public class Rect { public double width; public double height; Rect( ...

  6. java webservice 总结(学会读别人的webservice并且通过代理模式访问)

    公司做的系统之间的交互用到了webservice做交互,现在对webservice做一个总结. 1.配置已有的webservice webservice主要包括 xml/json:作为传输数据的格式 ...

  7. Java 线程 — ConcurrentHashMap

    ConcurrentHashMap ConcurrentHashMap 结构 采用了分段锁的方法提高COncurrentHashMap并发,一个map里面有一个Segment数组--即多个Segmen ...

  8. 分享一个 C# Winfrom 下的 OutlookBar 控件的使用

    最近在上网的时候,发现了这个C# 下的 OutlookBar 控件,看了一下感觉还真不错,特此记录一下. using System; using System.Drawing; using Syste ...

  9. Java多线程系列--“JUC集合”05之 ConcurrentSkipListMap

    概要 本章对Java.util.concurrent包中的ConcurrentSkipListMap类进行详细的介绍.内容包括:ConcurrentSkipListMap介绍ConcurrentSki ...

  10. [c++] Associative Containers

    关联容器 和 顺序容器 的本质差别在于: 关联容器通过键(key)存储和读取元素,而顺序容器则通过元素在容器中的位置顺序存储和访问元素. Reference: http://www.cnblogs.c ...