C#中的Excel范围格式帮助 [英] Excel range formatting Help in C#
本文介绍了C#中的Excel范围格式帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我无法通过C#代码格式化Excel工作表.我的代码是
Hi there,
I am not able to format my excel sheet through my C# code. My code is
try
{
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(path);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlApp.DisplayAlerts = false;
//Excel.Range excelCell = xlApp.get_Range("A1", "F10");
Excel.Range excelCell = (Excel.Range)xlWorkSheet.get_Range("A1", "F10");
excelCell.NumberFormat = "@";
//Excel.Ranges excelCell1 = xlApp.get_Range("H1", "O10");
Excel.Range excelCell1 = (Excel.Range)xlWorkSheet.get_Range("H1", "O10");
excelCell1.NumberFormat = "@";
//xlWorkBook.SaveAs(path, Excel.XlFileFormat.xlXMLSpreadsheet, misValue, misValue,
//false, false, Excel.XlSaveAsAccessMode.xlExclusive,misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Save();
xlWorkBook.Close(false, false, misValue);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
}
catch (Exception ex)
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet);
MessageBox.Show(ex.Message);
}
谢谢
推荐答案
请尝试导出Excel,如下面的文章所示,您可以轻松设置其格式.
将数据表导出为具有C#格式的Excel [ ^ ]
Please try to export excel as in the below article, you can format easily.
Export DataTable to Excel with Formatting in C#[^]
希望您可以通过以下方式找到解决方案:总结C#Control Excel技能 [ ^ ]
此致
Hope You can find your solution by : Summarize C# Control Excel Skills[^]
Best Regards
这篇关于C#中的Excel范围格式帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文