導出Excel方法
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); }
}
導出Excel方法的更多相关文章
- GridView導出Excel
1.aspx頁面需要添加:EnableEventValidation="false" 實例:<%@ Page Language="C#" AutoEven ...
- C# gridview分頁導出excel
#region 导出Excel方法 //导出到Excel按钮 protected void btnExport_Click(object sender, EventArgs e) { Export(& ...
- MFC---导出 Excel 方法
本方法通过Excel驱动写入 请添加头文件 #include"afxdb.h" 第一步创建Excel文件 安装驱动 CString FileName = L"first. ...
- C#根據當前DataGridView查詢數據導出Excel
private void btnsuggestinfo_Click(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.C ...
- GridView導出Excel 解決亂碼問題
Response.Clear(); Response.Charset = "gb2312"; Response.Buffer = true; Response.AddHeader( ...
- Saiku更改導出文件的文件名(十九)
Saiku更改導出文件的文件名 Saiku查询完数据之后,可以以excel,pdf,csv等格式将数据导出,这里我们来讲一下怎么更改导出的文件名. 找到对应的导出方法所在的js文件: saiku-se ...
- .Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) 通过MVC控制器导出导入Excel文件(可用于java SSH架构)
.Net MVC 导入导出Excel总结(三种导出Excel方法,一种导入Excel方法) [原文地址] 通过MVC控制器导出导入Excel文件(可用于java SSH架构) public cl ...
- C++ builder 操作Excel方法(据网上资料整理)
c++ builder 操作Excel方法,下面是从网上找到的一些不错的方法,学习一下: 用OLE操作Excel(目前最全的资料)(04.2.19更新) 本文档部分资料来自互联网,大部分是ccrun( ...
- JUnit中测试异常抛出的方法
最近在做TWU关于TDD的作业,对JUnit中测试异常抛出的方法进行了一些学习和思考. 在进行单元测试的时候有的时候需要测试某一方法是否抛出了正确的异常.例如,我有一个方法,里面对一个List进行读取 ...
随机推荐
- 比特币钱包的bitcoin-cli 命令全集
A.一般性的命令 help ( "command" ) stopgetinfopinggetnettotalsgetnetworkinfogetpeerinfogetconnect ...
- I.MX6 RGB clock 和 data 重合
/*********************************************************************** * I.MX6 RGB clock 和 data 重合 ...
- BZOJ4259:残缺的字符串(FFT与字符串匹配)
很久很久以前,在你刚刚学习字符串匹配的时候,有两个仅包含小写字母的字符串A和B,其中A串长度为m,B串长度为n.可当你现在再次碰到这两个串时,这两个串已经老化了,每个串都有不同程度的残缺. 你想对这两 ...
- python名片管理系统V2
主程序: #! /usr/bin env python3 # -*- coding: utf-8 -*- # 项目三: # 1.要求:编写一个名片管理系统,功能如下: # 用户输入相对应的指令,实现对 ...
- Oracle “CONNECT BY” 用法
Oracle “CONNECT BY”是层次查询子句,一般用于树状或者层次结果集的查询.其语法是: [ START WITH condition ] CONNECT BY [ NOCYCLE ] co ...
- ChartCtrl源码剖析之——CChartTitle类
CChartTitle类顾名思义,该类用来绘制波形控件的标题,它处于该控件的区域,如下图所示: CChartTitle类的头文件. #if !defined(AFX_CHARTTITLE_H__499 ...
- mysql error:You can't specify target table for update in FROM clause
mysql中You can't specify target table for update in FROM clause错误的意思是说,不能先select出同一表中的某些值,再update这个表( ...
- Git学习二
一.在工作区和暂存区退回 在工作区: $git checkout -- filename 在暂存区: $git reset HEAD filename $git checkout -- filenam ...
- bzoj 3270: 博物馆【dp+高斯消元】
好像是高斯消元解互相推(?)的dp的例子 首先考虑dp,设f[i][j]为一人在i一人在j的概率,点i答案显然就是f[i][i]: 然后根据题意,得到转移是 \[ f[i][j]=f[i][j]*p_ ...
- poj 2187【旋转卡壳模板】
求平面最远点对 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath& ...