Aspose.Cells相应操作

Aspose.Cells相应操作

1,上传
1.1 Workbook
Workbook workBook = new Workbook();
属性:
名称 值类型 说明
Colors Color[] 获取或设置Excel颜色
ConvertNumericData bool 获取或设置是否将字符串转换至数字数据
默认值为 true
DataSorter DataSorter 获取或设置数据分级
Date1904 bool
DefaultStyle Aspose.Cells.Style 获取或设置工作簿默认样式
HasMacro bool 获取工作簿是否包含宏观调控或宏
IsHScrollBarVisible bool 获取或设置左部滚动条(控制行)
默认值为true
IsProtected bool 获取工作簿保护状态
IsVScrollBarVisible bool 获取或设置底部滚动条(控制列)
默认值为true
Language CountryCode --枚举类型 获取或设置语言
默认为当前计算机区域
Password string 获取或设置工作簿密码
ReCalcOnOpen bool 获取或设置是否重新计算所有打开文件的公式
Region CountryCode --枚举类型 获取或设置工作簿区域(指当前使用者区域)
默认为当前计算机区域
Shared bool 获取或设置当前工作簿是否共享
默认为false
ShowTabs bool 获取或设置是否显示标签(工作表标签)
默认为true
Styles Styles 样式集合
Worksheets Worksheet
事件:
CalculateFormula(bool ignoreError
,ICustomFunction customFunction) +3 void 计算公式
ChangePalette(Color color,int index) void 设置当前颜色在调色版中显示顺序
Combine(Workbook secondWorkbook) void 联合工作簿,将secondWorkbook 工作簿中workSheet追加到当前工作簿中
Copy(Workbook source) void 拷贝工作簿到当前工作簿
Decrypt(string password) void 解除工作簿密码
IsColorInPalette(Color color) bool 将color加入到当前Excel调色版
LoadData(string fileName)
LoadData(System.IO.Stream stream) void 加载Excel到当前Workbook中
Open(string fileName,
FileFormatType.Default,
string password ); +8 void 打开Excel文件
Protect(ProtectionType.All,
string password); void 写保护,并设置取消工作簿保护密码
RemoveExternalLinks() void 移除外部链接
RemoveMacro() void 移除宏
Replace (string PlaceHolder,
string newValue); +8 void 工作簿中类型和值完全符合的单元格,将其替换为新值或对象
Save(Server.UrlEncode("测试.xls"),
FileFormatType.Default, SaveType.OpenInExcel, Response);+8 Void 保存工作簿
SaveToStream() System.IO.MemoryStream 将工作簿写入内存流中
Unprotect(string password); Void 取消工作簿保护状态
ValidateFormula(string formula) bool 验证公式
-----------

 

using System;
using System.Collections.Generic;
using System.Text;
using Aspose.Cells;
using System.Data;

namespace CRM.Common
{
    public class AsposeExcel
    {
        private string outFileName = "";
        private Workbook book = null;
        private Worksheet sheet = null;
        private log4net.ILog log = log4net.LogManager.GetLogger(typeof(AsposeExcel));

        public AsposeExcel(string outfilename,string tempfilename)
        {
            outFileName = outfilename;
            book = new Workbook();
            book.Open(tempfilename);
            sheet = book.Worksheets[0];
        }

        private void AddTitle(string title, int columnCount)
        {
            sheet.Cells.Merge(0, 0, 1, columnCount);
            sheet.Cells.Merge(1, 0, 1, columnCount);

            Cell cell1 = sheet.Cells[0, 0];
            cell1.PutValue(title);
            cell1.Style.HorizontalAlignment = TextAlignmentType.Center;
            cell1.Style.Font.Name = "黑体";
            cell1.Style.Font.Size = 14;
            cell1.Style.Font.IsBold = true;

            Cell cell2 = sheet.Cells[1, 0];
            cell1.PutValue("查询时间:" + DateTime.Now.ToLocalTime());
            cell2.SetStyle(cell1.Style);
        }

        private void AddHeader(DataTable dt)
        {
            Cell cell = null;
            for (int col = 0; col < dt.Columns.Count; col++)
            {
                cell = sheet.Cells[0, col];
                cell.PutValue(dt.Columns[col].ColumnName);
                cell.Style.Font.IsBold = true;
            }
        }

        private void AddBody(DataTable dt)
        {
            for (int r = 0; r < dt.Rows.Count; r++)
            {
                for (int c = 0; c < dt.Columns.Count; c++)
                {
                    sheet.Cells[r + 3, c].PutValue(dt.Rows[r][c].ToString());
                }
            }
        }

        public void DatatableToExcel(DataTable dt)
        {
            try
            {
                //sheet.Name = sheetName;
               
                //AddTitle(title, dt.Columns.Count);
                //AddHeader(dt);
                AddBody(dt);

                sheet.AutoFitColumns();
                //sheet.AutoFitRows();

                book.Save(outFileName);
            }
            catch (Exception e)
            {
                log.Error("导出Excel失败!" + e.Message);
                throw e;
            }
        }
    }
}

 

导入就不说了。导入为datetable之后就自己操作就OK

转载于:https://www.cnblogs.com/lhuser/articles/1756774.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值