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软件的功能了。
核心代码如下:
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);
}
}
{
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工作表中的一个或多个单元格
核心代码如下:
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);
}
}
{//保存为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);
}
}
- C#中如何保存文本为Word文件或Excel文件
- C#中如何保存文本为Word文件或Excel文件 [Z]
- 如何将网页数据输出为Excel或Word文件
- 如何将网页数据输出为Excel或Word文件
- 如何将网页数据输出为Excel或Word文件
- 如何将网页数据输出为Excel或Word文件
- 如何将网页数据输出为Excel或Word文件
- C# word或者excel保存或导出为PDF
- VC中如何将文件保存为EXCEL格式(CSpreadSheet)
- 在VC中如何将文件保存为EXCEL格式
- C# WinForm中如何保存一个文本文件或cs文件
- C# 用NPOI保存为Excel文件
- 如何通过C#保存一篇Excel文件?
- C# WinForm中如何将文件保存在文件夹中或如何新建文件夹来保存
- 保存为Excel文件
- ★ 如何将网页数据输出为Excel或Word文件
- (JSP)如何将网页数据输出为Excel或Word文件
- C# 读取Excel文件 并保存为html文件
- TNS: 监听程序未找到适用于客户机连接的例程
- asp.net 2.0小tips:在web.config里设置用户定义控件
- VS2005打包时加入卸载功能
- 浏览器的开发爱好者看过来
- 一个电话能改变什么?
- C#中如何保存文本为Word文件或Excel文件
- sprintf用法
- 存储过程教程(一)
- 35岁前成功的12条黄金法则
- ASP.Net全局变量的设置和读取方法
- .net之旅-partial(2)
- 十四个优秀网页设计审核站
- 40种网站设计常用技巧
- 妙用Cache检验用户是否重复登陆