C#.net VSTO Excel开发中一些常用方法

在excel开发中一些常用的方法。

获取选择的单元格

Excel.Range r = (Excel.Range)Application.ActiveCell;

获取指定单元格
wks是当前活动的Worksheet
Excel.Range cell1 = (Excel.Range)wks.Cells[next_row, 1];


删除一行
Excel.Range sel_row_range = (Excel.Range)wks.Rows["2:2", Type.Missing];
 //删除第二行
sel_row_range.Delete(Excel.XlDeleteShiftDirection.xlShiftUp);


设置range颜色
            Excel.Range range = this.Cells[1, 1];
            range.Interior.Color = 0x00ff00;


0x0000ff 红色
0x00ff00 绿色
0xff0000 蓝色


设置单元格计算函数
//显示计算结果的单元格
            Excel.Range math_range = get_Range("A5","A5");
//设置单元格表达式
            math_range.FormulaR1C1 = "=CalculateArea(R[-2]C[-1]:R[-2]C)";
R[-2]C[-1]:R[-2]C
意思是,从当前单元格开始算行数为当前行-2,列数为当前列-1,到达单元格为当前行-2,和当前列相等


设置单元格类型
//设置单元格类型为文本
cell2.NumberFormatLocal = "@";

设置公式
例 E3=B3*D3
Excel.Range r = (Excel.Range)wks.cells[3,5];
r.FormulaR1C1 = "=RC[-3]*RC[-1]";


设置区域字体
        private void SetRangeFont(Excel.Range range, string font_name, int font_size)
        {
            Excel.Font target_font = range.Font;
            target_font.Name = font_name;
            target_font.Size = font_size;
            target_font.Strikethrough = false;
            target_font.Superscript = false;
            target_font.Subscript = false;
            target_font.OutlineFont = false;
            target_font.Shadow = false;
            target_font.Underline = Excel.XlUnderlineStyle.xlUnderlineStyleNone;
            target_font.ThemeColor = Excel.XlThemeColor.xlThemeColorLight1;
            target_font.TintAndShade = 0;
            target_font.ThemeFont = Excel.XlThemeFont.xlThemeFontNone;
        }


设置选择单元格
            Excel.Range range = this.Cells[1, 1];
            range.Select();


设置选择行
            Excel.Range range = (Excel.Range)this.Rows[1, missing];
            range.Select();


通过代号获取range
Excel.Range range = get_Range("A1", "B3");


设置Range的详细属性,包括颜色,是否边框为黑色实线等

        private void SetRangeColor(Excel.Range range,Excel.XlThemeColor color)
        {
            range.Interior.Pattern = Excel.XlPattern.xlPatternSolid;
            range.Interior.PatternColorIndex = Excel.XlPattern.xlPatternAutomatic;
            range.Interior.ThemeColor = color;
            range.Interior.TintAndShade = 0.89999;
            range.Interior.PatternTintAndShade = 0;
            range.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            range.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            range.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            range.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].ColorIndex = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].TintAndShade = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
            range.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            range.Borders[Excel.XlBordersIndex.xlDiagonalDown].LineStyle = Excel.XlLineStyle.xlLineStyleNone;
            range.Borders[Excel.XlBordersIndex.xlDiagonalUp].LineStyle = Excel.XlLineStyle.xlLineStyleNone;


            range.Borders[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
            range.Borders[Excel.XlBordersIndex.xlEdgeLeft].ColorIndex = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeLeft].TintAndShade = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
            range.Borders[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
            range.Borders[Excel.XlBordersIndex.xlEdgeTop].ColorIndex = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeTop].TintAndShade = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;
            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].ColorIndex = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].TintAndShade = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
            range.Borders[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
            range.Borders[Excel.XlBordersIndex.xlEdgeRight].ColorIndex = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeRight].TintAndShade = 0;
            range.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
            range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
            range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex = 0;
            range.Borders[Excel.XlBordersIndex.xlInsideVertical].TintAndShade = 0;
            range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Excel.XlLineStyle.xlContinuous;
            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = 0;
            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].TintAndShade = 0;
            range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight = Excel.XlBorderWeight.xlThin;
        }


隐藏
                    Excel.Range child_range = (Excel.Range)wks.Rows["3:3", Type.Missing];
                    child_range.EntireRow.Hidden = true;


另存并加载到当前
wk.SaveAs(file_path, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);


另存不加载到当前
wk.SaveCopyAs(file_path);


合并单元格
        private void MergeCells(string col_name, int start_row, int end_row)
        {
            Excel.Worksheet wks = m_excell_app.ActiveSheet as Excel.Worksheet;
            string str1 = col_name + start_row.ToString();
            string str2 = col_name + end_row.ToString();
            Excel.Range r1 = (Excel.Range)wks.get_Range(str1, str2);
            r1.Merge(Type.Missing);
        }


设置居中,靠左
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;


设置上锁区域
            Excel.Worksheet wks = m_excell_app.ActiveSheet as Excel.Worksheet;
            foreach (Excel.AllowEditRange aer in wks.Protection.AllowEditRanges)
            {
                aer.Delete();
            }
            wks.Protection.AllowEditRanges.Add("s", (Excel.Range)wks.Columns["C:H", Type.Missing], Type.Missing);
            wks.Protection.AllowEditRanges.Add("s1", (Excel.Range)wks.Columns["X:AR", Type.Missing], Type.Missing);
            int i = 2;
            foreach (Excel.Range e in wks.UsedRange.Rows)
            {
                Excel.Range cell = (Excel.Range)wks.Cells[e.Row, 17];
                if (cell.Value2 == null)
                {
                    wks.Protection.AllowEditRanges.Add("s" + i.ToString(), cell, Type.Missing);
                    i++;
                }
                else
                {
                    try
                    {
                        Convert.ToDouble(cell.FormulaR1C1.ToString());
                        wks.Protection.AllowEditRanges.Add("s" + i.ToString(), cell, Type.Missing);
                        i++;
                    }
                    catch (Exception ex)
                    {


                    }
                }
            }
            wks.Protect(Type.Missing, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false);

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VSTO(Visual Studio Tools for Office)是一种用于在Microsoft Office应用程序创建自定义功能的开发工具。对于ExcelVSTO提供了一种使用.NET代码向工作簿写入数据的方法。要在Excel使用VSTO写入数据,可以按照以下步骤进行操作: 1. 首先,确保已经安装了Visual Studio和VSTO工具。 2. 在Visual Studio创建一个新的VSTO Excel项目。 3. 在项目编写代码,以将数据写入Excel。可以使用VBA或其他.NET语言(如C#或VB.NET)编写代码。 4. 使用VSTO提供的API来操作Excel对象模型,将数据写入到指定的单元格或工作表。 5. 在代码设置连接到MySQL数据库的参数,将数据从数据库读取并写入Excel。可以使用ADO.NET或其他适当的技术来连接和操作数据库。 6. 最后,在Excel显示写入的数据,可以通过在工作表创建新的数据表或使用现有的工作表来展示数据。 需要注意的是,VSTO需要在运行时系统上安装Office软件,并引用相应的.dll文件。除此之外,可以根据具体需求和性能要求选择合适的方法来进行数据写入。可以参考引用的链接了解更多关于往Excel写入数据的方法和技巧。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [使用VBA将excel的数据存放到mysql数据库里面,并将内容显示到sheet数据表](https://download.csdn.net/download/weixin_43050480/87211992)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [VSTOExcel快速写入数据的一种方法](https://blog.csdn.net/banquedou0439/article/details/101659316)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [利用VSTO实现向Excel模版指定位置写入数据](https://download.csdn.net/download/wicky0607/7407509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值