一, 如何保存文本为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);
}
}


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

【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞