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

posted @ 2010-06-12 08:32  Daniel_Lu  阅读(2395)  评论(0编辑  收藏  举报