C#操作Excel之写入特殊字符

一、添加引用

直接搜索Excel寻找引用,找不到多半是电脑没安装office

二、定义Excel工作表并写入特殊字符

特殊字符是特殊字体Wingdings下的字符,在宋体下是字符,所以可以直接复制粘贴至代码中,再通过设置字体实现在Excel表中写入特殊符号。

try
            {
                #region 复制记录表,定义Excel工作表
                //复制记录表
                string file_model_path = startPath + "\\记录表\\记录表.xlsx";
                string file_path = startPath + "\\TEMP\\记录表.xlsx";
                System.IO.File.Copy(file_model_path, file_path, true);
                //定义Excel工作表
                Microsoft.Office.Interop.Excel.Application xApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
                Microsoft.Office.Interop.Excel.Workbook xBook = xApp.Workbooks.Open(file_path, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
                Microsoft.Office.Interop.Excel.Worksheet xSheet = xBook.Sheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
                Microsoft.Office.Interop.Excel.Range rng1 = xSheet.get_Range("A1", System.Type.Missing);
                #endregion

                #region 将内容写入Excel工作表
                rng1 = xSheet.get_Range("A3", System.Type.Missing);
                rng1.Value2 = "特殊字符写入测试";
                if (model == "勾选1")
                {
                    rng1 = xSheet.get_Range("H4", System.Type.Missing);
                    rng1.Value2 = "1þ" + "\n" + "2□";//将特殊字符复制粘贴至此
                    rng1.Font.Name = "Wingdings";//直接设置字体即可
                }
                else
                {
                    rng1 = xSheet.get_Range("H4", System.Type.Missing);
                    rng1.Value2 = "1□" + "\n" + "2þ";
                    rng1.Font.Name = "Wingdings";
                }

                #endregion

                xBook.Close(true);
                MessageBox.Show("导出完毕!");
                if (xApp != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(xApp);
                    xApp = null;
                }
                GC.Collect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                System.Diagnostics.Process[] excelprocess = System.Diagnostics.Process.GetProcessesByName("EXCEL");
                foreach (System.Diagnostics.Process p in excelprocess)
                    p.Kill();
                ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown();
            }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值