導出Excel方法
2024-09-04 09:12:08
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices; namespace WmsClient
{
public class Excel
{
public static void SaveAsExcel(DataTable dtExcel)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "导出Excel (*.xls)|*.xls";
saveFileDialog.FilterIndex = ;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
string strName = saveFileDialog.FileName;
if (strName.Length != )
{
//导出到execl
System.Reflection.Missing miss = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
try
{
excel.Application.Workbooks.Add(true);
excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
if (excel == null)
{
MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
sheet.Name = "test"; int m = , n = ;
//生成列名称 这里i是从1开始的 因为我第0列是个隐藏列ID 没必要写进去
for (int i = ; i < dtExcel.Columns.Count; i++)
{
excel.Cells[, i + ] = dtExcel.Columns[i].Caption.ToString();
} //填充数据
for (int i = ; i < dtExcel.Rows.Count; i++)
{
//j也是从1开始 原因如上 每个人需求不一样
for (int j = ; j < dtExcel.Columns.Count; j++)
{
if (dtExcel.Rows[i][j].ToString().GetType() == typeof(string))
{
excel.Cells[i + , j + ] = "'" + dtExcel.Rows[i][j].ToString().Trim();
}
else
{
excel.Cells[i + , j + ] = dtExcel.Rows[i][j].ToString().Trim();
}
}
} sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
book.Close(false, miss, miss);
books.Close();
excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); GC.Collect();
MessageBox.Show("数据已经成功导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//toolStripProgressBar1.Value = 0;
System.Diagnostics.Process.Start(strName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误提示");
}
finally
{
KillSpecialExcel(excel);
}
}
} private static void KillSpecialExcel(Microsoft.Office.Interop.Excel.Application m_objExcel)
{
try
{
if (m_objExcel != null)
{
int lpdwProcessId;
GetWindowThreadProcessId(new IntPtr(m_objExcel.Hwnd), out lpdwProcessId);
System.Diagnostics.Process.GetProcessById(lpdwProcessId).Kill();
}
}
catch (Exception ex)
{
Console.WriteLine("Delete Excel Process Error:" + ex.Message);
}
} [DllImport("User32.dll", CharSet = CharSet.Auto)]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); }
}
最新文章
- SOUI中做的一个磁力吸附效果
- linux的压缩命令
- onethink常用标签的使用示例
- 【收藏用】--切勿转载JAVA 使用Dom4j 解析XML
- Windows Phone 8开发环境搭建
- SQL性能优化之一
- des算法的C#实现
- 关于appStore评分的相关说明--转自张诚教授
- Restart-ServiceEx.psm1
- hibernate 对 sql server 2005 分页改进
- Cherrypy文件上传非ASCII文件名乱码问题解决
- python模块的使用
- 【Java每日一题】20170221
- Spring HttpInvoker 从实战到源码追溯
- mysql密码的查看/修改
- 输入系统:epoll &; inotify
- RMQ_ST表
- windows 远程连接
- 关于OBS获取显示器黑屏的解决办法
- log4net日志的简单配置
热门文章
- CMake使用总结【转】
- BZOJ_3172_[Tjoi2013]单词_AC自动机
- [Codeforces 1011E] Border
- 洛谷 P1071 潜伏者 —— 模拟
- saltstack源码-启动3-config.py配置文件加载
- Java-Runoob-高级教程-实例-数组:02. Java 实例 – 数组添加元素
- bzoj 1089: [SCOI2003]严格n元树【dp+高精】
- bzoj 4814: [Cqoi2017]小Q的草稿【计算几何】
- 洛谷 P2766 最长不下降子序列问【dp+最大流】
- UTF-8格式的文本文件程序读取异常