WinForm导出文件为Word、Excel、文本文件的方法
作者:junjie 时间:2022-07-08 20:07:16
好久没有写文章了,下面把自己最近程序中用到的一个小小的导出文件的方法给在家分享一下,欢迎大家来排砖,谢谢~不说废话了,直接上代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Office.Interop.Word;
using System.IO;
using Microsoft.Office.Interop.Excel;
using Sun.Winform.Util;
namespace Sun.Winform.Files
{
/// <summary>
/// 将内容导出为文件类。
/// </summary>
/// <remarks>
/// 作者:SunYujing
/// 日期:2011-12-18
/// </remarks>
public class ExportFile
{
/// <summary>
/// 将字符串存储为word文档格式的文件的方法(多线程)。
/// </summary>
/// <param name="strText">要保存的字符串内容。</param>
public static void SaveAsWord(string p_str)
{
Thread thread = new Thread(SaveAsWordFile);
thread.SetApartmentState(ApartmentState.STA);
thread.Start(p_str);
}
/// <summary>
/// 将字符串存储为txt格式的文件的方法(多线程)。
/// </summary>
/// <param name="p_str"></param>
public static void SaveAsTxt(string p_str)
{
Thread thread = new Thread(SaveAsTxtFile);
thread.SetApartmentState(ApartmentState.STA);
thread.Start(p_str);
}
/// <summary>
/// 导出数据表数据到Excel(多线程)。
/// </summary>
public static void SaveAsExcel(System.Data.DataTable dataTable)
{
if (dataTable == null)
{
MessageUtil.ShowError("请先指定要导出的数据表");
return;
}
Thread thread = new Thread(SaveAsExcelTableFile);
thread.SetApartmentState(ApartmentState.STA);
thread.Start(dataTable);
}
/// <summary>
/// 导出数据集数据到Excel(多线程)。
/// </summary>
public static void SaveAsExcel(System.Data.DataSet dataSet)
{
if (dataSet == null)
{
MessageUtil.ShowError("请先指定要导出的数据集");
return;
}
Thread thread = new Thread(SaveAsExcelSetFile);
thread.SetApartmentState(ApartmentState.STA);
thread.Start(dataSet);
}
/// <summary>
/// 将字符串存储为word文档格式的文件。
/// </summary>
/// <param name="strtext">要保存的字符串内容。</param>
private static void SaveAsWordFile(object strtext)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择文件存放路径";
sfd.FileName = "导出数据";
sfd.Filter = "Word文档(*.doc)|*.doc";
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
string FileName = sfd.FileName.ToLower();
if (!FileName.Contains(".doc"))
{
FileName += ".doc";
}
if (FileName.Substring(FileName.LastIndexOf(Path.DirectorySeparatorChar)).Length <= 5)
{
MessageUtil.ShowThreadMessage("文件保存失败,文件名不能为空!");
return;
}
try
{
DateTime start = DateTime.Now;
MessageUtil.ShowThreadMessage("正在保存文件,请稍候...");
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 = strtext.ToString();
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);
GC.Collect();
DateTime end = DateTime.Now;
TimeSpan ts = end - start;
MessageUtil.ShowMessage("文件保存成功,用时" + ts.ToString());
}
catch (System.Exception ex)
{
MessageUtil.ShowError(ex.Message);
}
}
/// <summary>
/// 将字符串存储为txt文档格式的文件。
/// </summary>
/// <param name="strtext">要保存的字符串内容。</param>
private static void SaveAsTxtFile(object strtext)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择文件存放路径";
sfd.FileName = "导出数据";
sfd.Filter = "文本文档(*.txt)|*.txt";
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
string FileName = sfd.FileName.ToLower();
if (!FileName.Contains(".txt"))
{
FileName += ".txt";
}
if (FileName.Substring(FileName.LastIndexOf(Path.DirectorySeparatorChar)).Length <= 5)
{
MessageUtil.ShowThreadMessage("文件保存失败,文件名不能为空!");
return;
}
try
{
DateTime start = DateTime.Now;
StreamWriter sw = new StreamWriter(FileName, false);
sw.Write(strtext.ToString());
sw.Flush();
sw.Close();
DateTime end = DateTime.Now;
TimeSpan ts = end - start;
MessageUtil.ShowMessage("文件保存成功,用时" + ts.ToString());
}
catch (Exception ex)
{
MessageUtil.ShowError(ex.Message);
}
}
/// <summary>
/// 将数据存储为Excel文件。
/// </summary>
/// <param name="p_dt">要保存的数据表。</param>
private static void SaveAsExcelTableFile(object p_dt)
{
System.Data.DataTable dt = (System.Data.DataTable)p_dt;
if (dt.Rows.Count == 0)
{
MessageUtil.ShowError("没有可保存的数据");
return;
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择文件存放路径";
sfd.FileName = "导出数据";
sfd.Filter = "Excel文档(*.xls)|*.xls";
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
string FileName = sfd.FileName.ToLower();
if (!FileName.Contains(".xls"))
{
FileName += ".xls";
}
if (FileName.Substring(FileName.LastIndexOf(Path.DirectorySeparatorChar)).Length <= 5)
{
MessageUtil.ShowThreadMessage("文件保存失败,文件名不能为空!");
return;
}
if (sfd.FileName != "")
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
if (excelApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机器未安装Excel");
return;
}
else
{
MessageUtil.ShowThreadMessage("正在导出数据,请稍候...");
DateTime start = DateTime.Now;
Microsoft.Office.Interop.Excel.Workbooks workbooks = excelApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
for (int col = 1; col <= dt.Columns.Count; col++)
{
worksheet.Cells[1, col] = dt.Columns[col - 1].Caption.ToString();
}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dt.Rows[i][j].ToString();
}
}
workbook.Saved = true;
workbook.SaveCopyAs(sfd.FileName);
//释放资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
workbooks = null;
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
excelApp = null;
//使用垃圾回收可以关闭EXCEL.EXE进程
GC.Collect();
DateTime end = DateTime.Now;
int iTimeSpan = (end.Minute - start.Minute) * 60 + (end.Second - start.Second);
MessageUtil.ShowMessage("数据导出完毕,用时" + iTimeSpan.ToString() + "秒");
}
}
}
/// <summary>
/// 将数据集存储为Excel文件。
/// </summary>
/// <param name="p_ds">要导出的数据集。</param>
private static void SaveAsExcelSetFile(object p_ds)
{
System.Data.DataSet ds = (System.Data.DataSet)p_ds;
if (ds == null || ds.Tables.Count == 0)
{
MessageUtil.ShowError("没有可保存的数据");
return;
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "请选择文件存放路径";
sfd.FileName = "导出数据";
sfd.Filter = "Excel文档(*.xls)|*.xls";
if (sfd.ShowDialog() != DialogResult.OK)
{
return;
}
string FileName = sfd.FileName.ToLower();
if (!FileName.Contains(".xls"))
{
FileName += ".xls";
}
if (FileName.Substring(FileName.LastIndexOf(Path.DirectorySeparatorChar)).Length <= 5)
{
MessageUtil.ShowThreadMessage("文件保存失败,文件名不能为空!");
return;
}
if (sfd.FileName != "")
{
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
if (excelApp == null)
{
MessageBox.Show("无法创建Excel对象,可能您的机器未安装Excel");
return;
}
else
{
MessageUtil.ShowThreadMessage("正在导出数据,请稍候...");
DateTime start = DateTime.Now;
Microsoft.Office.Interop.Excel.Workbooks workbooks = excelApp.Workbooks;
Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
object objMissing = System.Reflection.Missing.Value;
for (int m = 0; m < ds.Tables.Count; m++)
{
System.Data.DataTable dt = ds.Tables[m];
worksheet = (Worksheet)workbook.ActiveSheet;
worksheet.Name = dt.TableName;
for (int col = 1; col <= dt.Columns.Count; col++)
{
worksheet.Cells[1, col] = dt.Columns[col - 1].Caption.ToString();
}
for (int i = 1; i <= dt.Rows.Count; i++)
{
for (int j = 1; j <= dt.Columns.Count; j++)
{
worksheet.Cells[i + 1, j] = dt.Rows[i - 1][j - 1].ToString();
}
}
if (m < ds.Tables.Count - 1)
{
workbook.Sheets.Add(objMissing, objMissing, 1, XlSheetType.xlWorksheet);
}
}
workbook.Saved = true;
workbook.SaveCopyAs(sfd.FileName);
//释放资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
worksheet = null;
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
workbooks = null;
excelApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
excelApp = null;
GC.Collect();
DateTime end = DateTime.Now;
int iTimeSapn = (end.Minute - start.Minute) * 60 + (end.Second - start.Second);
MessageUtil.ShowMessage("数据导出完毕,用时" + (iTimeSapn / 60).ToString() + "分" + (iTimeSapn % 60).ToString() + "秒");
}
}
}
}
}
标签:WinForm,导出文件,Word,Excel,文本文件
0
投稿
猜你喜欢
Java服务器主机信息监控工具类的示例代码
2023-07-26 04:34:49
Windows 10上JDK环境安装配置图文教程
2023-05-31 19:38:03
C#实现递归调用的Lambda表达式
2022-10-03 05:13:29
JAVA版排序算法之快速排序示例
2023-04-20 04:37:53
C#实现获取设置IP地址小工具
2022-08-21 18:06:48
Android实现简单画图画板
2022-04-02 12:52:56
Spring学习笔记1之IOC详解尽量使用注解以及java代码
2021-10-07 16:17:02
在Android项目中使用AspectJ的方法
2023-02-01 23:33:54
Java的MD5工具类和客户端测试类
2022-04-23 03:08:48
Java 实战项目锤炼之校园宿舍管理系统的实现流程
2023-10-15 07:00:58
Android列表组件ListView使用详解之动态加载或修改列表数据
2023-10-01 16:14:38
C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】
2022-09-14 20:41:14
Linux下用java -jar运行可执行jar包的方法教程
2022-11-24 18:01:46
Spring boot随机端口你都不会还怎么动态扩容
2021-09-29 10:10:14
Android实现屏幕录制功能
2022-10-29 15:26:36
Java Swing中的文本框(JTextField)与文本区(JTextArea)使用实例
2022-08-15 00:17:46
Java网络编程UDP实现多线程在线聊天
2022-01-08 21:48:32
Spring容器注册组件实现过程解析
2023-07-10 11:08:07
SpringBoot整合Pulsar的实现示例
2021-10-09 17:39:35
java中如何获取时间戳的方法实例
2021-06-14 15:29:16