首页 > 编程 > .NET > 正文

asp.net导出Excel类库代码分享

2024-07-10 12:44:13
字体:
来源:转载
供稿:网友

代码如下:
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Web;
using Excel = Microsoft.Office.Interop.Excel;

/// <summary>
///ExcelClass 的摘要说明
/// </summary>
public class ExcelClass
{
    /// <summary>
    /// 构建ExcelClass类
    /// </summary>
    public ExcelClass()
    {
        this.m_objExcel = new Excel.Application();
    }
    /// <summary>
    /// 构建ExcelClass类
    /// </summary>
    /// <param name="objExcel">Excel.Application</param>
    public ExcelClass(Excel.Application objExcel)
    {
        this.m_objExcel = objExcel;
    }

    /// <summary>
    /// 列标号
    /// </summary>
    private string AList = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    /// <summary>
    /// 获取描述区域的字符
    /// </summary>
    /// <param name="x"></param>
    /// <param name="y"></param>
    /// <returns></returns>
    public string GetAix(int x, int y)
    {
        char[] AChars = AList.ToCharArray();
        if (x >= 26) { return ""; }
        string s = "";
        s = s + AChars[x - 1].ToString();
        s = s + y.ToString();
        return s;
    }

    /// <summary>
    /// 给单元格赋值1
    /// </summary>
    /// <param name="x">行号</param>
    /// <param name="y">列号</param>
    /// <param name="align">对齐(CENTER、LEFT、RIGHT)</param>
    /// <param name="text">值</param>
    public void setValue(int y, int x, string align, string text)
    {
        Excel.Range range = sheet.get_Range(this.GetAix(x, y), miss);
        range.set_Value(miss, text);
        if (align.ToUpper() == "CENTER")
        {
            range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表