一、添加引用
直接搜索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();
}
,