返回顶部

收藏

Asp.Net 读写Excel数据代码

更多

首先需要保证服务器上有excel的com组件,我使用的是“Microsoft Excel 11.0 Object Library”。

如下代码实现读写单元格数据:

using Microsoft.Office.Interop.Excel;  

// Creates a new Excel Application Object  
ApplicationClass xlsApp = new ApplicationClass();    

try  
{  
    //Opens WorkBook in Excel  
    WorkbookClass xlsWrkBook=(WorkbookClass)xlsApp.Workbooks.Open  
 ("c:\\1.xls", 0, false, 5, "", "", true,  
 XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);  
    //Gets the sheet 1 of the opened workbook    
    Worksheet xlsWrkSht = (Worksheet)xlsWrkBook.Worksheets.get_Item(1);  
    //Gets Cell 1st Row and 1st Column. Desired can be given   
    Range ExcelCellText = (Range)xlsWrkSht.Cells[1, 1];  
    // Reads Data of selected cell  
    string str = ExcelCellText.Text.ToString();  
    // Writes Data of selected cell   
    ExcelCellText.Value2 = "Sample Write";  
    xlsWrkBook.Save();  
    xlsWrkBook.Close(0, 0, 0);  
}    
catch  
{ 
  //error handle
}
finally  
{  
    xlsApp = null;  
}  

标签:Asp.Net,excel,c#

收藏

0人收藏

支持

0

反对

0

发表评论