c#操作Excel文件(office 11.0 object library)

该博客为转载内容,转载自https://www.cnblogs.com/froster/archive/2005/03/30/128822.html ,涉及C#相关知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gifusing Microsoft.Office.Interop.Excel;
None.gif
namespace FileHandler
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class ExcelFile
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public ExcelFile(string fileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!Initial())
InBlock.gif                
return;
InBlock.gif
InBlock.gif            excelFileName 
= fileName;
InBlock.gif 
InBlock.gif            
//加入新的WorkBook
InBlock.gif            
//获取WorkBooks集合
InBlock.gif

InBlock.gif            workbooks 
= excelApp.Workbooks;
InBlock.gif            
if(!System.IO.File.Exists(fileName)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{//create a new xls file(XlWBATemplate.xlWBATWorksheet
InBlock.gif
                workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
InBlock.gif                
//*修改原有文件
InBlock.gif
                workbook = workbooks.Add(excelFileName);
InBlock.gif
InBlock.gif            
//获取WorkSheets集合
InBlock.gif
            sheets = workbook.Worksheets;
InBlock.gif            worksheet 
= (Worksheet) sheets.get_Item(1); 
InBlock.gif            
if (worksheet == null
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                MessageBox.Show(
"worksheet is null!","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }
 
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Dispose()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(isInitialed && excelApp != null)
InBlock.gif            excelApp.Quit();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public bool Initial()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!isInitialed)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                excelApp 
= new Microsoft.Office.Interop.Excel.Application();
InBlock.gif                
if (excelApp == null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    MessageBox.Show(
"Excel couldn't be started!","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif                }

InBlock.gif                excelApp.DisplayAlerts 
= false;
InBlock.gif                isInitialed 
= true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private static Microsoft.Office.Interop.Excel.Application excelApp;
InBlock.gif        
private Microsoft.Office.Interop.Excel.Workbooks workbooks;
InBlock.gif        
private Microsoft.Office.Interop.Excel.Workbook workbook;
InBlock.gif        
private Sheets sheets;
InBlock.gif        
private Worksheet worksheet;
InBlock.gif        
private string excelFileName;
InBlock.gif        
private static bool isInitialed = false;
InBlock.gif
InBlock.gif        
public void Write(int row, int column, object val)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            worksheet.Cells[row, column] 
= val;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public object Read(int row, int column)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return worksheet.Cells[row, column];
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void Save()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!workbook.Saved)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                workbook.Close(
true,excelFileName,true);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/froster/archive/2005/03/30/128822.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值