程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 使用C#和Excel進行報表開發(7)

使用C#和Excel進行報表開發(7)

編輯:關於C語言

在Excel中,如果某個單元格的內容是數字,且比較長,默認情況下,會變為科學計數發表示,例如:123456789123456000會被表示為1.2345E+17,為了能夠將數字按照普通的格式完全表示,就需要設置單元格的屬性 ,下面是例子代碼:

Excel.Application m_objExcel = null;
Excel._Workbook m_objBook = null;
Excel.Sheets m_obJSheets = null;
Excel._Worksheet m_obJSheet = null;
Excel.Range m_objRange = null;
object m_objOpt = System.Reflection.Missing.Value;
try
{
m_objExcel = new Excel.Application();
m_objBook = m_objExcel.Workbooks.Open("e:\\1.xls", m_objOpt, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, m_objOpt);
m_obJSheets = (Excel.Sheets)m_objBook.Worksheets;
m_objSheet = (Excel._Worksheet)(m_obJSheets.get_Item(1));
m_objRange = m_obJSheet.get_Range("A1", m_objOpt);
m_objRange.NumberFormatLocal = "#";
m_objBook.SaveAs("e:\\22.xls", m_objOpt, m_objOpt,
m_objOpt, m_objOpt, m_objOpt, Excel.XlSaveAsAccessMode.xlNoChange,
m_objOpt, m_objOpt, m_objOpt, m_objOpt, m_objOpt);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
m_objBook.Close(m_objOpt, m_objOpt, m_objOpt);
m_objExcel.Workbooks.Close();
m_objExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
m_objBook = null;
m_objExcel = null;
GC.Collect();
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved