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

来源:互联网 发布:java 开发框架设计 编辑:程序博客网 时间:2024/06/05 11:46
一,           如何保存文本为Word文件
 
要在.net中操作Word,就需要在项目中引用Word的对象库文件MSWORD.OLB,这可以在office安装目录下找到(C:/Program Files/Microsoft Office/OFFICE11/MSWORD.OLB,把它引入项目中就可以使用Word对象的各种方法来实现Word软件的功能了。

06121406.JPG

 
核心代码如下:


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

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

        }



二,如何保存为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

核心代码如下:

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

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

                        MyData[lvi.Index,
2= lvi.SubItems[2].Text;
                    }

                    myRange 
= myRange.get_Resize(this.listView1.Items.Count,3);

                    myRange.Value2 
= MyData;
                    myRange.EntireColumn.AutoFit();

                }

                myExcel 
= null;

            }

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

        }