C#中如何保存文本为Word文件或Excel文件 [Z]

本文介绍如何使用C#操作Word和Excel文件,包括创建、编辑及保存文档的基本方法。

一,           如何保存文本为Word文件<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

要在.net中操作Word,就需要在项目中引用Word的对象库文件MSWORD.OLB,这可以在office安装目录下找到(C:\Program Files\Microsoft Office\OFFICE11\MSWORD.OLB,把它引入项目中就可以使用Word对象的各种方法来实现Word软件的功能了。

06121406.JPG

核心代码如下:



None.gif    private void button1_Click(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
if(this.richTextBox1.Text=="")
InBlock.gif                
return;
InBlock.gif            
if(this.saveFileDialog1.ShowDialog()==DialogResult.Cancel)
InBlock.gif                
return;
InBlock.gif            
string FileName = this.saveFileDialog1.FileName;
InBlock.gif            
if(FileName.Length<1)
InBlock.gif                
return;
InBlock.gif            FileName
+=".doc";
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Microsoft.Office.Interop.Word.ApplicationClass word 
= new Microsoft.Office.Interop.Word.ApplicationClass();
InBlock.gif                Microsoft.Office.Interop.Word.Document doc;
InBlock.gif                
object nothing  = System.Reflection.Missing.Value;
InBlock.gif                doc 
= word.Documents.Add(ref nothing,ref nothing,ref nothing,ref nothing);
InBlock.gif                doc.Paragraphs.Last.Range.Text 
= this.richTextBox1.Text;
InBlock.gif                
object myfileName = FileName;
InBlock.gif        
//将WordDoc文档对象的内容保存为doc文档
InBlock.gif
                        doc.SaveAs(ref myfileName,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing,ref nothing);
InBlock.gif    
//关闭WordDoc文档对象
InBlock.gif
                doc.Close(ref nothing,ref nothing,ref nothing);
InBlock.gif
//关闭WordApp组件对象
InBlock.gif
                word.Quit(ref nothing,ref nothing,ref nothing);
InBlock.gif                MessageBox.Show(
"Word文件保存成功","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
this,ex.Message.ToString(),"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif
None.gif


二,如何保存为Excel文件

 

要在.net中操作Excel,就需要在项目中引用EXCEL.EXE这可以在office安装目录下找到(C:\Program Files\Microsoft Office\OFFICE11\EXCEL.EXE

Excel对象使用最多的是下面4个:

Application对象,处于Excel对象层次结构的顶层,表示 Excel自身的运行环境。

Workbook对象,直接处于Application的下层,表示一个Excel工作簿文件

Worksheet对象,包含在Workbook对象中,表示一个Excel工作表

Range对象,包含在Worksheet中,表示Excel工作表中的一个或多个单元格

06121408.JPG

核心代码如下:

None.gif    private void button1_Click(object sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{//保存为Excel文件
InBlock.gif
            if(this.listView1.Items.Count<1)
InBlock.gif                
return;
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Microsoft.Office.Interop.Excel.ApplicationClass myExcel 
= new Microsoft.Office.Interop.Excel.ApplicationClass();
InBlock.gif                myExcel.Visible 
= true;
InBlock.gif                
if(myExcel==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    MessageBox.Show(
"Excel无法启动!!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                Microsoft.Office.Interop.Excel.Workbooks myWorkBooks 
= myExcel.Workbooks;
InBlock.gif                Microsoft.Office.Interop.Excel.Workbook myWorkBook 
= myWorkBooks.Add(System.Reflection.Missing.Value);
InBlock.gif                Microsoft.Office.Interop.Excel.Worksheet myWorkSheet 
= (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Worksheets[1];
InBlock.gif                Microsoft.Office.Interop.Excel.Range myRange 
= myWorkSheet.get_Range("A1","C1");
ExpandedSubBlockStart.gifContractedSubBlock.gif                
object[] myHeader = dot.gif{"姓名","专业","毕业院校"};
InBlock.gif                myRange.Value2 
= myHeader;
InBlock.gif                
if(this.listView1.Items.Count>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    myRange 
= myWorkSheet.get_Range("A2",System.Reflection.Missing.Value);
InBlock.gif                    
object[,] MyData = new object[this.listView1.Items.Count,3];
InBlock.gif                    
foreach(ListViewItem lvi in this.listView1.Items)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MyData[lvi.Index,
0= lvi.Text;
InBlock.gif                        MyData[lvi.Index,
1= lvi.SubItems[1].Text;
InBlock.gif
InBlock.gif                        MyData[lvi.Index,
2= lvi.SubItems[2].Text;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    myRange 
= myRange.get_Resize(this.listView1.Items.Count,3);
InBlock.gif
InBlock.gif                    myRange.Value2 
= MyData;
InBlock.gif                    myRange.EntireColumn.AutoFit();
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                myExcel 
= null;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(System.Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MessageBox.Show(
this,ex.Message.ToString(),"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif

转载于:https://www.cnblogs.com/RobotTech/archive/2008/02/14/1068791.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值