【ArcGIS Pro二次开发】(85):Aspose.Cells中的Excel操作

Aspose.Cells是一款功能强大的Excel文档处理和转换控件,开发人员和客户电脑无需安装Microsoft Excel也能在应用程序中实现类似Excel的强大数据管理功能。


1、获取工作薄Workbook
string excelFile = "C:\Users\Administrator\Desktop\FE.xlsx";
Workbook wb = OpenWorkbook(excelFile);
2、获取工作表对象Worksheet
// 按序号
Worksheet sheet = wb.Worksheets[0];
// 按sheet名
Worksheet sheet = wb.Worksheets["sheet1"];
3、获取Cells、Cell
// 获取Cells
Cells cells = sheet.Cells
// 获取Cell,序号从0开始
Cell cell = cells[1,2]
4、读取、写入Cell值
// 读取cell值
var va = cell.Value;
string va = cell.StringValue;
...
// 写入cell值
cell.Value = "这是一个写入值";
cell.PutValue("写入值");
5、获取并设置单元格样式
// 获取style
Style style = cell.GetStyle();
// 数字型
style.Number = 4;
// 数字格式
style.Custom = "0.00";

// 设置style
cell.SetStyle(style);

更多详细设置:

style.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style.Font.Name = "宋体";//文字字体
style.Font.Size = 22;//文字大小
style.IsLocked = false;//单元格解锁
style.Font.IsBold = true;//粗体
style.ForegroundColor = Color.FromArgb(0xaa, 0xcc, 0xbb);//设置背景色
style.Pattern = BackgroundType.Solid; //设置背景样式
style.IsTextWrapped = true;//单元格内容自动换行
style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; //应用边界线 左边界线
style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; //应用边界线 右边界线
style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin; //应用边界线 上边界线
style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin; //应用边界线 下边界线
6、保存Workbook并释放
// 保存
wb.Save(excelFile);
wb.Dispose();
7、删除行
// 获取Cells
Cells cells = sheet.Cells
// 删除单行,序号从0开始
cells.DeleteRow(3);
// 删除多行,参数2为要删的行数
cells.DeleteRows(3,10);
8、删除列
// 获取Cells
Cells cells = sheet.Cells
// 删除列,序号从0开始
cells.DeleteColumn(3);
// 删除多列,参数2为要删的列数
cells.DeleteColumns(3,10);
9、复制行(复制列同理)
// 获取Cells
Cells cells = sheet.Cells
// 复制单行,从参数2复制到参数3
cells.CopyRow(cells, 83, 85);
// 复制多行,参数4为要复制的行数
cells.CopyRows(cells, 83, 168 + i * 85, 85);
10、强制更新计算公式、设置公式
//强制更新计算公式
wb.CalculateFormula();
//给单元格设置计算公式
cell.Formula = "=AVERAGE(B1:E1)";
11、设置行高列宽
//设置行高
cells.SetRowHeight(0, 20); 
//设置列宽
cells.SetColumnWidth(1, 30);
12、合并、取消合并单元格
// 参数:起始行,起始列,行数,列数
cells.Merge(0, 0, 1, 5);
// 参数:起始行,起始列,行数,列数
cells.UnMerge(4, 2, 2, 3);
13、插入行、列
// 在序号处插入一行
cells.InsertRow(5);
// 参数2为插入的行数
cells.InsertRows(5,3);

cells.InsertColumn(5);
cells.InsertColumns(5,3);
14、将字典dict的值映射写入Excel文件
public static void ExcelAttributeMapper(string excelPath, int sheet_in_col, int sheet_map_col, Dictionary<string, string> dict, int startRow = 0)
{
    // 打开工作薄
    Workbook wb = OpenWorkbook(excelPath);
    // 打开工作表
    Worksheet sheet = wb.Worksheets[0];
    // 逐行处理
    for (int i = startRow; i <= sheet.Cells.MaxDataRow; i++)
    {
        //  获取目标cell
        Cell inCell = sheet.Cells[i, sheet_in_col];
        Cell mapCell = sheet.Cells[i, sheet_map_col];
        // 属性映射
        if (inCell is not null && dict.ContainsKey(inCell.StringValue))
        {
            mapCell.Value = dict[inCell.StringValue];   // 赋值
        }
    }
    // 保存
    wb.Save(excelFile);
    wb.Dispose();
}
15、简单的写入值
public static void ExcelWriteCell(string excelPath, int row, int col, string cell_value)
{
    // 打开工作薄
    Workbook wb = OpenWorkbook(excelPath);
    // 打开工作表
    Worksheet sheet = wb.Worksheets[0];
    // 获取cell
    Cell cell = sheet.Cells[row, col];
    // 写入cell值
    cell.Value = cell_value;
    // 保存
    wb.Save(excelFile);
    wb.Dispose();
}
16、Excel设置单元格的格式
public static void ExcelSetColStyle(string excelPath, int col, int startRow, int styleNumber = 4, int digit = 2)
{
    // 打开工作薄
    Workbook wb = OpenWorkbook(excelPath);
    // 打开工作表
    Worksheet sheet = wb.Worksheets[0];

    for (int i = startRow; i <= sheet.Cells.MaxDataRow; i++)
    {
        // 获取cell
        Cell cell = sheet.Cells[i, col];
        // 获取style
        Style style = cell.GetStyle();
        // 数字型
        style.Number = styleNumber;
        // 小数位数
        if (digit == 1) { style.Custom = "0.0"; }
        else if (digit == 2) { style.Custom = "0.00"; }
        else if (digit == 3) { style.Custom = "0.000"; }
        else if (digit == 4) { style.Custom = "0.0000"; }
    }

    // 保存
    wb.Save(excelFile);
    wb.Dispose();
}
17、删除Excel表中的0值行【指定多个列】
private static List<int> ExcelDeleteNullRowResult(string excelPath, List<int> deleteCols, int startRow = 0)
{
    List<int> list = new List<int>();

    // 打开工作薄
    Workbook wb = OpenWorkbook(excelPath);
    // 打开工作表
    Worksheet sheet = wb.Worksheets[0];

    // 强制更新表内的公式单元格
    wb.CalculateFormula();

    // 找出0值行
    for (int i = sheet.Cells.MaxDataRow; i >= startRow; i--)
    {
        // 设置一个flag
        bool isNull = true;
        // 循环查找各列的值
        foreach (var deleteCol in deleteCols)
        {
            Cell cell = sheet.Cells.GetCell(i, deleteCol);
            if (cell != null)  // 值不为空
            {
                string str = cell.StringValue;
                if (str != "") // 值不为0
                {
                    if (double.Parse(str) != 0)
                    {
                        isNull = false;
                        break;
                    }
                }
            }
        }
        // 输出删除列
        if (isNull)
        {
            list.Add(i);
        }
    }
    // 保存
    wb.Save(excelFile);
    wb.Dispose();
    // 返回值
    return list;
}
18、从Excel文件中获取Dictionary
public static Dictionary<string, string> GetDictFromExcel(string excelPath, int col1 = 0, int col2 = 1)
{
    // 定义字典
    Dictionary<string, string> dict = new Dictionary<string, string>();
    // 打开工作薄
    Workbook wb = OpenWorkbook(excelPath);
    // 打开工作表
    Worksheet sheet = wb.Worksheets[0];
    // 获取key和value值
    for (int i = 0; i <= sheet.Cells.MaxDataRow; i++)
    {
        Cell key = sheet.Cells[i, col1];
        Cell value = sheet.Cells[i, col2];
        if (key != null && value != null)
        {
            if (!dict.ContainsKey(key.StringValue))
            {
                if (key.StringValue != "" && value.StringValue != "")   // 空值不纳入
                {
                    dict.Add(key.StringValue, value.StringValue);
                }
            }
        }
    }
    wb.Dispose();
    // 返回dict
    return dict;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

规划GIS会

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值