C# aspose.cell 操作代码实例,封装类

有同事问我有个项目需要Excel文当的操作,有什么好的代码示例,
看了一下,自己根据
DocumentFormat.OpenXml.dll
类来写这样的功能实现不太现实,工作量太大,可能还会有好多问题,
由于时间原因,做了个对aspose.cell第三方功能类的封装来实现。
aspose.cell 对excel操作的功能 还是挺强大的,但是功能 代码示例不太好找,网上一段一段的。
所以自己把找到的一些功能列在一起封装了一下。
分享给大家,
**

代码如下:

**
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MY.COM.Doc
{
public class Excel
{
private string _docPath;

    private Aspose.Cells.Workbook _doc;

    private Aspose.Cells.Worksheet _sheet;

    /// 
    /// 初始化excel操作对像
    /// 
    public Excel()
    {
        _docPath = System.Web.HttpContext.Current.Server.MapPath("/") + "Temp/" + DateTime.Now.ToString("yyMM") + "/COM/Doc/Excel/";
        IO.Directory.createIfUnExist(_docPath);
        _docPath += DateTime.Now.Ticks.ToString() + ".xlsx";
        _doc = new Aspose.Cells.Workbook();
        if (_doc.Worksheets.Count < 1)
            createSheet("Sheet1");
        else
            _sheet = _doc.Worksheets[0];
    }

    /// 
    /// 初始化excel操作对像
    /// 
    /// excel文件地址
    public Excel(string docPath)
    {
        this._docPath = docPath;
        _doc = new Aspose.Cells.Workbook(_docPath);
        if (_doc.Worksheets.Count < 1)
            createSheet("Sheet1");
        else
            _sheet = _doc.Worksheets[0];


    }

    /// 
    /// 初始化excel操作对像
    /// 
    /// excel文件地址
    /// 文档保护密码,如果文档加了密码的情况
    public Excel(string docPath, string pwd)
    {
        this._docPath = docPath;
        _doc = new Aspose.Cells.Workbook(_docPath, new Aspose.Cells.LoadOptions() { Password = pwd });
        if (_doc.Worksheets.Count < 1)
            createSheet("Sheet1");
        else
            _sheet = _doc.Worksheets[0];


    }

    /// 
    /// 创建一个Sheet
    /// 
    /// Sheet名称
    /// 
    public string createSheet(string sheetName)
    {
        _sheet = _doc.Worksheets.Add(sheetName);
        return "OK:";
    }

    /// 
    /// 切换到指定Sheet
    /// 
    /// Sheet的索引序号0开始
    /// 
    public string selectSheet(int sheetIndex)
    {
        if (_doc.Worksheets.Count < sheetIndex - 1)
            return "ERR:超过Sheet表索引";
        _sheet = _doc.Worksheets[sheetIndex];
        return "OK:";
    }

    /// 
    /// 切换到指定Sheet
    /// 
    /// Sheet名称
    /// 
    public string selectSheet(string sheetName)
    {
        _sheet = _doc.Worksheets[sheetName];
        return "OK:";
    }

    /// 
    /// 设置为保护文档
    /// 
    /// 保护密码
    /// 
    public string toProtect(string pwd = "")
    {
        _doc.Protect(Aspose.Cells.ProtectionType.All, pwd);
        //_sheet.Protect(Aspose.Cells.ProtectionType.All, newPwd, oldPwd);//保护工作表
        //_sheet.Protection.AllowSelectingLockedCell = false;//设置只能选择解锁单元格
        //_sheet.Protection.AllowInsertingColumn = true; //设置可以调整列
        //_sheet.Protection.AllowFormattingRow = true; //设置可以调整行
        return "OK:";
    }

    public string cellsMerge(cellInfo cellStart, cellInfo cellEnd)
    {
        Aspose.Cells.Cells cells = _sheet.Cells;
        cells.Merge(cellStart.rowIndex, cellStart.colIndex, cellEnd.rowIndex - cellStart.rowIndex + 1, cellEnd.colIndex - cellStart.colIndex + 1);//合并行列
        return "OK:";
    }

    public string cellsMerge(string cellNameStart, string cellNameEnd)
    {
        Aspose.Cells.Cells cells = _sheet.Cells;
        cellInfo cellStart = new cellInfo(cellNameStart);
        cellInfo cellEnd = new cellInfo(cellNameEnd);
        cells.Merge(cellStart.rowIndex, cellStart.colIndex, cellEnd.rowIndex - cellStart.rowIndex + 1, cellEnd.colIndex - cellStart.colIndex + 1);//合并行列
        //cells.Merge(3, 3, 2, 2);
        return "OK:";
    }

    public Aspose.Cells.Style getStyle()
    {
        Aspose.Cells.Style style = _doc.CreateStyle();
        style.Pattern = Aspose.Cells.BackgroundType.Solid;
        return style;
    }

    public Aspose.Cells.Style getStyle(string cellName)
    {
        Aspose.Cells.Style style = _sheet.Cells[cellName].GetStyle();
        style.Pattern = Aspose.Cells.BackgroundType.Solid;
        return style;
    }

    public Aspose.Cells.Style getStyle(int rowNum, int colNum)
    {
        Aspose.Cells.Style style = _sheet.Cells[rowNum - 1, colNum - 1].GetStyle();
        style.Pattern = Aspose.Cells.BackgroundType.Solid;
        return style;
    }

    public Aspose.Cells.Style getStyle(cellInfo cell)
    {
        Aspose.Cells.Style style = _sheet.Cells[cell.rowIndex, cell.colIndex].GetStyle();
        style.Pattern = Aspose.Cells.BackgroundType.Solid;
        return style;
    }

    private Aspose.Cells.Style getStyleExample()
    {
        Aspose.Cells.Style style = _doc.CreateStyle();
        style.Pattern = Aspose.Cells.BackgroundType.Solid;//默认添加,否则背景色设置可能存在无效情况
        style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center; //水平设置居中
        style.VerticalAlignment = Aspose.Cells.TextAlignmentType.Center;//垂直设置居中
        style.ForegroundColor = System.Drawing.Color.Green;  //设置背景颜色
        style.Font.Name = "宋体";//文字字体 
        style.Font.Size = 15;//文字大小 
        style.Font.IsBold = true;//粗体 
        style.IsTextWrapped = true;//单元格内容自动换行  
        style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线 
        style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线 
        style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线 
        style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线
        style.Custom = "0.0%";

        //举出的Aspose.Cells提供的内嵌显示格式:
        //Value Type Format String
        //0 General General
        //1 Decimal 0
        //2 Decimal 0.00
        //3 Decimal #,##0
        //4 Decimal #,##0.00
        //5 Currency $#,##0;$-#,##0
        //6 Currency $#,##0;[Red]$-#,##0
        //7 Currency $#,##0.00;$-#,##0.00
        //8 Currency $#,##0.00;[Red]$-#,##0.00
        //9 Percentage 0 %
        //10 Percentage 0.00 %
        //11 Scientific 0.00E+00
        //12 Fraction # ?/?
        //13 Fraction # /
        //14 Date m/ d / yy
        //15 Date d-mmm - yy
        //16 Date d-mmm
        //17 Date mmm-yy
        //18 Time h:mm AM/ PM
        //19 Time h:mm: ss AM/ PM
        //20 Time h:mm
        //21 Time h:mm: ss
        //22 Time m/ d / yy h: mm
        //37 Currency #,##0;-#,##0
        //38 Currency #,##0;[Red]-#,##0
        //39 Currency #,##0.00;-#,##0.00
        //40 Currency #,##0.00;[Red]-#,##0.00
        //41 Accounting _ * #,##0_ ;_ * "_ ;_ @_
        //42 Accounting _ $* #,##0_ ;_ $* "_ ;_ @_
        //43 Accounting _ * #,##0.00_ ;_ * "??_ ;_ @_
        //44 Accounting _ $* #,##0.00_ ;_ $* "??_ ;_ @_
        //45 Time mm:ss
        //46 Time h :mm: ss
        //47 Time mm:ss.0
        //48 Scientific ##0.0E+00
        //49 Text @

        return style;
    }

    public string setStyle(Aspose.Cells.Style style, string cellNameStart, string cellNameEnd)
    {
        return setStyle(style, new cellInfo(cellNameStart), new cellInfo(cellNameEnd));
    }
    public string setStyle(Aspose.Cells.Style style, cellInfo cellStart, cellInfo cellEnd)
    {
        Aspose.Cells.Range w = _sheet.Cells.CreateRange(cellStart.rowIndex, cellStart.colIndex, cellEnd.rowIndex - cellStart.rowIndex + 1, cellStart.colIndex - cellEnd.colIndex + 1);
        w.ApplyStyle(style, new Aspose.Cells.StyleFlag() { All = true });
        return "OK:";
    }

    public string setStyle(Aspose.Cells.Style style, string cellName)
    {
        return setStyle(style, new cellInfo(cellName));
    }
    public string setStyle(Aspose.Cells.Style style, cellInfo cell)
    {
        _sheet.Cells[cell.rowIndex, cell.colIndex].SetStyle(style);
        return "OK:";
    }



    public string insertTxt(string cellName, string txt, Aspose.Cells.Style style = null)
    {
        _sheet.Cells[cellName].PutValue(txt);
        if (style != null)
            setStyle(style, cellName);
        return "OK:";
    }

    public string insertTxt(cellInfo cell, string txt, Aspose.Cells.Style style = null)
    {
        _sheet.Cells[cell.rowIndex, cell.colIndex].PutValue(txt);
        if (style != null)
            setStyle(style, cell);
        return "OK:";
    }

    public string insertNumber(string cellName, double number, Aspose.Cells.Style style = null)
    {
        _sheet.Cells[cellName].PutValue(number);
        if (style != null)
            setStyle(style, cellName);
        return "OK:";
    }

    public string insertNumber(cellInfo cell, double number, Aspose.Cells.Style style = null)
    {
        _sheet.Cells[cell.rowIndex, cell.colIndex].PutValue(number);
        if (style != null)
            setStyle(style, cell);
        return "OK:";
    }

    public string save(string savePath, Aspose.Cells.SaveFormat sf = Aspose.Cells.SaveFormat.Xlsx)
    {
        _doc.Save(savePath, sf);
        return "OK:";
    }

    public string getLoadUrl()
    {
        _doc.Save(_docPath, Aspose.Cells.SaveFormat.Xlsx);
        string reUrl = _docPath;
        reUrl = reUrl.Replace(System.Web.HttpContext.Current.Server.MapPath("/"), "/");
        return "OK:" + reUrl;
    }

    public Aspose.Cells.FontSetting getCellSectionFontSetting(string cellName, int indexS, int length)
    {
        //_sheet.Cells["S4"].Characters(2, 1).Font.Name = "Wingdings 2";
        return _sheet.Cells[cellName].Characters(indexS, length);
    }

    public Aspose.Cells.FontSetting getCellSectionFontSetting(cellInfo cell, int indexS, int length)
    {
        //_sheet.Cells["S4"].Characters(2, 1).Font.Name = "Wingdings 2";
        return _sheet.Cells[cell.rowIndex, cell.colIndex].Characters(indexS, length);
    }

    public Aspose.Cells.Drawing.Picture insertImg(string uperLeftCellName, string imgPath)
    {
        return insertImg(new cellInfo(uperLeftCellName), imgPath);
    }
    public Aspose.Cells.Drawing.Picture insertImg(cellInfo uperLeftCell, string imgPath)
    {
        int iIndex = _sheet.Pictures.Add(uperLeftCell.rowIndex, uperLeftCell.colIndex, imgPath);
        Aspose.Cells.Drawing.Picture pic = _sheet.Pictures[iIndex];
        //pic.Placement = Aspose.Cells.Drawing.PlacementType.FreeFloating;
        //pic.BorderLineColor = System.Drawing.Color.Red;
        //pic.BorderWeight = 5;
        //pic.Left = left;
        //pic.Top = top;
        return pic;
    }

    public Aspose.Cells.Drawing.Picture insertImg(string uperLeftCellName, string lowerRightCellName, string imgPath)
    {
        return insertImg(new cellInfo(uperLeftCellName), new cellInfo(lowerRightCellName), imgPath);
    }
    public Aspose.Cells.Drawing.Picture insertImg(cellInfo uperLeftCell, cellInfo lowerRightCell, string imgPath)
    {
        int iIndex = _sheet.Pictures.Add(uperLeftCell.rowIndex, uperLeftCell.colIndex, lowerRightCell.rowIndex, lowerRightCell.colIndex, imgPath);
        Aspose.Cells.Drawing.Picture pic = _sheet.Pictures[iIndex];
        return pic;
    }

    public Aspose.Cells.Drawing.Picture insertImg(string uperLeftCellName, string imgPath, int width, int height)
    {
        return insertImg(new cellInfo(uperLeftCellName), imgPath, width, height);
    }
    public Aspose.Cells.Drawing.Picture insertImg(cellInfo uperLeftCell, string imgPath, int width, int height)
    {
        int iIndex = _sheet.Pictures.Add(uperLeftCell.rowIndex, uperLeftCell.colIndex, imgPath, width, height);
        Aspose.Cells.Drawing.Picture pic = _sheet.Pictures[iIndex];
        return pic;
    }

    public Aspose.Cells.PageSetup getPageSet()
    {
        Aspose.Cells.PageSetup pageSet = _sheet.PageSetup;

        //pageSet.PrintArea = "$A$1:$C$5";//设置打印区域
        //pageSet.Order = Aspose.Cells.PrintOrderType.DownThenOver;//xlDownThenOver =向下处理行,然后向右逐个处理页或页面字段。先列后行 /xlOverThenDown =向右逐个处理页或页面字段,然后向下处理行。先行后列
        //pageSet.PrintGridlines = true;//是否打印网格线
        //pageSet.PrintTitleRows = ""; //此属性与“顶端标题行”选项对应。如果仅指定行的一部分,Microsoft Excel 将把该区域扩展为整个行。将该属性设置为 False 或空字符串(""),将会关闭标题行。
        //pageSet.PrintTitleColumns = ""; //此属性与“左端标题行”选项对应。如果仅指定列的一部分,Microsoft Excel 将自动把该区域扩展为整个列。(加一个例子)将该属性设置为 False 或空字符串(""),将会关闭标题列。
        //pageSet.PrintComments = Aspose.Cells.PrintCommentsType.PrintNoComments;
        xlPrintInPlace        16        批注打印在其插入工作表的位置。        如同工作表中的显示
        xlPrintNoComments - 4142        不打印批注。(默认)        (空)
        xlPrintSheetEnd        1        批注打印为工作表末尾的尾注。        工作表末尾
        //pageSet.PrintHeadings = false;//如果打印本页时同时打印行标题和列标题,则该值为True。仅应用于工作表。Boolean类型,可读写。此属性与“打印-行号列标”选项对应。
        //pageSet.BlackAndWhite = false;//如果指定文档中的元素以黑白方式打印,则该属性值为True。Boolean类型,可读写。此属性与“打印-单色打印”选项对应。
        //pageSet.PrintErrors = Aspose.Cells.PrintErrorsType.PrintErrorsNA;
        xlPrintErrorsBlank         1        打印错误为空白。            < 空白 >
        xlPrintErrorsDash          2        打印错误显示为划线。        --
        xlPrintErrorsDisplayed    0        显示全部打印错误。        显示值
        xlPrintErrorsNA            3        打印错误显示为不可用。   #N/A
        //pageSet.PrintDraft = false;//如果打印工作表时不打印其中的图形,则该属性值为True。Boolean类型,可读写。此属性与“打印-草稿品质”选项对应。

        //pageSet.PrintQuality = 600;//打印质量
        //pageSet.Orientation = Aspose.Cells.PageOrientationType.Landscape;//xlLandscape’设置模向打印模式  xlPortrait’设置纵向打印模式
        //pageSet.FirstPageNumber = 1;//起始页码
        //pageSet.Zoom = 100;//返回或设置一个Variant值,它代表一个数值在10%到400%之间的百分比,该百分比为MicrosoftExcel打印工作表时的缩放比例。此属性与“缩放比例”选项对应。输入时不要输入百分号(%),只输入数值就可以了。
        //pageSet.FitToPagesWide = 1;//返回或设置打印工作表时,对工作表进行缩放使用的页宽。仅应用于工作表。Variant类型,可读写。此属性与“页高”选项对应。
        //pageSet.FitToPagesTall = 1;//返回或设置打印工作表时,对工作表进行缩放使用的页高。仅应用于工作表。Variant类型,可读写。此属性与“页宽”选项对应。
        只有Zoom属性值为False时,FitToPagesWide和FitToPagesTall属性才会起作用,大家使用的时候请注意。
        //pageSet.PaperSize = Aspose.Cells.PaperSizeType.PaperA4;//返回或设置纸张的大小。XlPaperSize类型,可读写。此属性与“纸张大小”选项对应。

        //pageSet.CenterVertically = false;//如果在页面的垂直居中位置打印指定工作表,则该属性值为True。Boolean类型,可读写。此属性与“垂直”选项对应。
        //pageSet.CenterHorizontally = false;//如果在页面的水平居中位置打印指定工作表,则该属性值为True。Boolean类型,可读写。此属性与“水平”选项对应。
        //pageSet.TopMargin = 28.35;//以磅为单位返回或设置上边距的大小。Double类型,可读写。此属性与“上”选项对应。
        //pageSet.BottomMargin = 28.35;//以磅为单位返回或设置底端边距的大小。Double类型,可读写。此属性与“下”选项对应。
        //pageSet.RightMargin = 28.35;//以磅为单位返回或设置右边距的大小。Double类型,可读写。此属性与“右”选项对应。
        //pageSet.LeftMargin = 28.35;//以磅为单位返回或设置左边距的大小。Double类型,可读写。此属性与“左”选项对应。
        //pageSet.FooterMargin = 28.35;//以磅为单位返回或设置页脚到页面底端的距离。Double类型,可读写。此属性与“页脚”选项对应。
        //pageSet.HeaderMargin = 28.35;//以磅为单位返回或设置页面顶端到页眉的距离。Double类型,可读写。此属性与“页眉”选项对应。
        1磅约等于0.03527厘米   所以这里可以这样表示 2CM=2/0.03527


        //pageSet.IsHFDiffOddEven = false;//如果指定的PageSetup对象的奇数页和偶数页具有不同的页眉和页脚,则为True。可读 / 写Boolean类型。此属性与“奇偶页不同”选项对应。
        //pageSet.IsHFDiffFirst = false;//如果在第一页使用不同的页眉或页脚,则为True。可读 / 写Boolean类型。此属性与“首页不同”选项对应。如果属性为TRUE,则无法选择系统设置的页眉 / 页脚样式。需要使用FirstPage属性设置第一页的页眉 / 页脚。
        //pageSet.IsHFScaleWithDoc = true;//返回或设置页眉和页脚是否在文档大小更改时随文档缩放。可读 / 写Boolean类型。此属性与“随文档自动缩放”选项对应。
        //pageSet.IsHFAlignMargins = true;//如果Excel以页面设置选项中设置的边距对齐页眉和页脚,则返回True。可读 / 写Boolean类型。此属性与“与页边距对齐”选项对应。
        pageSet.SetFooterPicture


        return pageSet;

    }

    public string setRowHeight(int rowNum, int heightPixel)
    {
        _sheet.Cells.SetRowHeightPixel(rowNum - 1, heightPixel);
        return "OK:";
    }

    public string setColumnWidth(int colNum, int widthPixel)
    {
        _sheet.Cells.SetColumnWidthPixel(colNum - 1, widthPixel);
        return "OK:";
    }

    public string setColumnWidth(string colName, int widthPixel)
    {
        int colNum = 0;
        if (!System.Text.RegularExpressions.Regex.IsMatch(colName.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); }
        int index = 0;
        char[] chars = colName.ToUpper().ToCharArray();
        for (int i = 0; i < chars.Length; i++)
        {
            index += ((int)chars[i] - (int)'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
        }
        colNum += index;

        _sheet.Cells.SetColumnWidthPixel(colNum - 1, widthPixel);
        return "OK:";
    }
}

public struct cellInfo
{
    public int rowIndex { get; set; }
    public int colIndex { get; set; }
    public string cellName { get; set; }

    public cellInfo(int rowNum, string colName)
    {
        colName = colName.ToUpper();
        this.cellName = colName + rowNum.ToString();
        this.rowIndex = rowNum - 1;
        this.colIndex = -1;

        if (!System.Text.RegularExpressions.Regex.IsMatch(colName.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); }
        int index = 0;
        char[] chars = colName.ToUpper().ToCharArray();
        for (int i = 0; i < chars.Length; i++)
        {
            index += ((int)chars[i] - (int)'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
        }
        this.colIndex += index;
    }

    public cellInfo(int rowNum, int colNum)
    {
        this.rowIndex = rowNum - 1;
        this.colIndex = colNum - 1;
        this.cellName = GetColumnChar(colNum);
    }

    public cellInfo(string cellName)
    {
        this.cellName = cellName;
        string colNameStr = "";
        string rowNumStr = "";
        foreach (char c in cellName)
        {
            if (c >= 48 && c <= 57)
                rowNumStr += c;
            else
                colNameStr += c;
        }
        this.rowIndex = int.Parse(rowNumStr) - 1;
        this.colIndex = -1;

        if (!System.Text.RegularExpressions.Regex.IsMatch(colNameStr.ToUpper(), @"[A-Z]+")) { throw new Exception("invalid parameter"); }
        int index = 0;
        char[] chars = colNameStr.ToUpper().ToCharArray();
        for (int i = 0; i < chars.Length; i++)
        {
            index += ((int)chars[i] - (int)'A' + 1) * (int)Math.Pow(26, chars.Length - i - 1);
        }
        this.colIndex += index;
    }

    private static string GetColumnChar(int col)
    {
        var a = col / 26;
        var b = col % 26;

        if (a > 0) return GetColumnChar(a - 1) + (char)(b + 65);
        return ((char)(b + 65)).ToString();
    }


}

}

**

操作示代例代码下:

**
MY.COM.Doc.Excel myex1 = new MY.COM.Doc.Excel();//创建一个空的excel对像(默认创建Sheet1)
MY.COM.Doc.Excel myex2 = new MY.COM.Doc.Excel(Server.MapPath(“1.xlsx”));//打开一个现有的excel对像
MY.COM.Doc.Excel myex3 = new MY.COM.Doc.Excel(Server.MapPath(“1.xlsx”), “123456”);//打开一个现有的加密excel对像

        myex1.createSheet("Sheet2");//创建一个新的Sheet切换到当前Sheet
        myex1.selectSheet(0);//选择sheet,0=第一个,1=第二个
        myex1.selectSheet("Sheet1");//选择sheet,按sheet名称
        myex1.toProtect("123456");//设置当前文当保护密码
        myex1.cellsMerge("A1", "B2");//合并单元格操作
        var style1 = myex1.getStyle();//取得一个新的样式
        var style2 = myex1.getStyle("A1");//取得A1单元格样式

        //以下是样式设置示例
        style1.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center; //水平设置居中
        style1.VerticalAlignment = Aspose.Cells.TextAlignmentType.Center;//垂直设置居中
        style1.ForegroundColor = System.Drawing.Color.Green;  //设置背景颜色
        style1.Font.Name = "宋体";//文字字体 
        style1.Font.Size = 15;//文字大小 
        style1.Font.IsBold = true;//是否粗体 
        style1.IsTextWrapped = true;//单元格内容是否自动换行  
        style1.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 左边界线 
        style1.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 右边界线 
        style1.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 上边界线 
        style1.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //应用边界线 下边界线
        style1.Custom = "0.0%";//自定义格式

        myex1.setStyle(style1, "A1");//设置单元格样式
        myex1.setStyle(style1, "A1", "B2");//设置区域样式
        myex1.insertNumber("A1", 12.12);//写入数值
        myex1.insertNumber("A1", 12.12, style1);//写入数值,并设置当前单元格样式
        myex1.insertTxt("A4", "这里是测试的文本内容");//写入数值
        myex1.insertTxt("A5", "这里是测试的文本内容", style1);//写入数值,并设置当前单元格样式
        myex1.insertImg("C3", Server.MapPath("1.png"));//插入图片,C3单元格起,长和宽为图片长宽
        myex1.insertImg("E5", "F6", Server.MapPath("1.png"));//插入图片 大小为 E5起F6至范围
        myex1.insertImg("G7", Server.MapPath("1.png"), 100, 100);//插入图片G7起,指定长和宽

        myex1.setColumnWidth("E", 100);//设置第E列宽 100像素(Px)
        myex1.setRowHeight(1, 100);//设置第1号高 100像素(Px)

        var tempLoadPath = myex1.getLoadUrl();//取得下载文档的url地址 返回 OK:地址

        //以下是对单元格里 指定字符串内容的样式设置
        var secFond = myex1.getCellSectionFontSetting("A4", 3, 2);//取得A4单元格里第4个字符起2个字符内容,如"这里是测试的文本内容"中测试二字
        secFond.Font.Color = System.Drawing.Color.Red;//测试二字设置为红色
        secFond.Font.IsBold = true;//测试二字加粗

        var pageSet = myex1.getPageSet();//取得当前Sheet的页面打印布局对像
        pageSet.PrintErrors = Aspose.Cells.PrintErrorsType.PrintErrorsNA;//打印错误显示为不可用。   #N/A
        pageSet.PrintArea = "$A$1:$C$5";//设置打印区域
        pageSet.Zoom = 0;
        pageSet.FitToPagesWide = 1;//返回或设置打印工作表时,对工作表进行缩放使用的页宽。仅应用于工作表。Variant类型,可读写。此属性与“页高”选项对应。
        pageSet.FitToPagesTall = 1;//返回或设置打印工作表时,对工作表进行缩放使用的页高。仅应用于工作表。Variant类型,可读写。此属性与“页宽”选项对应。
        pageSet.PrintGridlines = true;//是否打印网格线
        pageSet.TopMargin = 2 / 0.03527;//返回或设置上边距的大小 2CM
        pageSet.BottomMargin = 2 / 0.03527;//返回或设置底端边距的大小 2CM
        pageSet.RightMargin = 2 / 0.03527;//返回或设置右边距的大小 2CM
        pageSet.LeftMargin = 2 / 0.03527;//返回或设置左边距的大小 2CM
        //1磅约等于0.03527厘米 所以这里可以这样表示 2CM = 2 / 0.03527

        myex1.save(Server.MapPath("1_save.xlsx"));//保存Excel文档
        myex1.save(Server.MapPath("1_save.pdf"), Aspose.Cells.SaveFormat.Pdf);//另存为PDF文档

你可能感兴趣的:(excel,c#,asp.net)