ExcelHelper Excel,Export,Import
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Odbc;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Excel = Microsoft.Office.Interop.Excel; namespace ExcelExportImport
{
public class ExcelHelper
{
private Excel.Application _excelApp = null;
private Excel.Workbooks _books = null;
private Excel._Workbook _book = null;
private Excel.Sheets _sheets = null;
private Excel._Worksheet _sheet = null;
private Excel.Range _range = null;
private Excel.Font _font = null;
// Optional argument variable
private object _optionalValue = Missing.Value; /// <summary>
/// 读取Excel文件
/// </summary>
/// <param name="pPath"></param>
/// <returns></returns>
public DataTable LoadExcel(string pPath)
{
//Driver={Driver do Microsoft Excel(*.xls)} 这种连接写法不需要创建一个数据源DSN,DRIVERID表示驱动ID,Excel2003后都使用790,FIL表示Excel文件类型,Excel2007用excel 8.0,MaxBufferSize表示缓存大小,DBQ表示读取Excel的文件名(全路径) string connString = "Driver={Driver do Microsoft Excel(*.xls)};DriverId=790;SafeTransactions=0;ReadOnly=1;MaxScanRows=16;Threads=3;MaxBufferSize=2024;UserCommitSync=Yes;FIL=excel 8.0;PageTimeout=5;";
connString += "DBQ=" + pPath;
OdbcConnection conn = new OdbcConnection(connString);
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = conn;
//获取Excel中第一个Sheet名称,作为查询时的表名
string sheetName = this.GetExcelSheetName(pPath);
string sql = "select * from [" + sheetName.Replace('.', '#') + "$]";
cmd.CommandText = sql;
OdbcDataAdapter da = new OdbcDataAdapter(cmd);
DataSet ds = new DataSet();
try
{
da.Fill(ds);
return ds.Tables[0];
}
catch (Exception x)
{
ds = null; throw new Exception("从Excel文件中获取数据时发生错误!");
}
finally
{
cmd.Dispose();
cmd = null;
da.Dispose();
da = null;
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn = null;
}
}
private string GetExcelSheetName(string pPath)
{
//打开一个Excel应用 _excelApp = new Excel.Application();
if (_excelApp == null)
{
throw new Exception("打开Excel应用时发生错误!");
}
_books = _excelApp.Workbooks;
//打开一个现有的工作薄
_book = _books.Add(pPath);
_sheets = _book.Sheets;
//选择第一个Sheet页
_sheet = (Excel._Worksheet)_sheets.get_Item(1);
string sheetName = _sheet.Name; ReleaseCOM(_sheet);
ReleaseCOM(_sheets);
ReleaseCOM(_book);
ReleaseCOM(_books);
_excelApp.Quit();
ReleaseCOM(_excelApp);
return sheetName;
}
/// <summary>
/// 释放COM对象
/// </summary>
/// <param name="pObj"></param>
private void ReleaseCOM(object pObj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(pObj);
}
catch
{
throw new Exception("释放资源时发生错误!");
}
finally
{
pObj = null;
}
}
///以下为导出实现功能
/// <summary>
/// 保存到Excel
/// </summary>
/// <param name="excelName"></param>
public void SaveToExcel(string excelName,DataTable dataTable)
{
try
{
if (dataTable != null)
{
if (dataTable.Rows.Count != 0)
{
Mouse.SetCursor(Cursors.Wait);
CreateExcelRef();
FillSheet(dataTable);
SaveExcel(excelName);
Mouse.SetCursor(Cursors.Arrow);
}
}
}
catch (Exception e)
{
MessageBox.Show("Error while generating Excel report");
}
finally
{
ReleaseCOM(_sheet);
ReleaseCOM(_sheets);
ReleaseCOM(_book);
ReleaseCOM(_books);
ReleaseCOM(_excelApp);
}
} /// <summary>
/// 将内存中Excel保存到本地路径
/// </summary>
/// <param name="excelName"></param>
private void SaveExcel(string excelName)
{
_excelApp.Visible = false;
//保存为Office2003和Office2007都兼容的格式
_book.SaveAs(excelName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
_excelApp.Quit(); } /// <summary>
/// 将数据填充到内存Excel的工作表
/// </summary>
/// <param name="dataTable"></param>
private void FillSheet(DataTable dataTable)
{
object[] header = CreateHeader(dataTable);
WriteData(header,dataTable);
} private void WriteData(object[] header,DataTable dataTable)
{
object[,] objData = new object[dataTable.Rows.Count, header.Length]; for (int j = 0; j < dataTable.Rows.Count; j++)
{
var item = dataTable.Rows[j];
for (int i = 0; i < header.Length; i++)
{
var y = dataTable.Rows[j][i];
objData[j, i] = (y == null) ? "" : y.ToString();
}
}
AddExcelRows("A2", dataTable.Rows.Count, header.Length, objData);
AutoFitColumns("A1", dataTable.Rows.Count + 1, header.Length);
} private void AutoFitColumns(string startRange, int rowCount, int colCount)
{
_range = _sheet.get_Range(startRange, _optionalValue);
_range = _range.get_Resize(rowCount, colCount);
_range.Columns.AutoFit();
} private object[] CreateHeader(DataTable dataTable)
{ List<object> objHeaders = new List<object>();
for (int n = 0; n < dataTable.Columns.Count; n++)
{
objHeaders.Add(dataTable.Columns[n].ColumnName);
} var headerToAdd = objHeaders.ToArray();
//工作表的单元是从“A1”开始
AddExcelRows("A6", 1, headerToAdd.Length, headerToAdd);
SetHeaderStyle(); return headerToAdd;
} /// <summary>
/// 将表头加粗显示
/// </summary>
private void SetHeaderStyle()
{
_font = _range.Font;
_font.Bold = true;
} /// <summary>
/// 将数据填充到Excel工作表的单元格中
/// </summary>
/// <param name="startRange"></param>
/// <param name="rowCount"></param>
/// <param name="colCount"></param>
/// <param name="values"></param>
private void AddExcelRows(string startRange, int rowCount, int colCount, object values)
{
_range = _sheet.get_Range(startRange, _optionalValue);
_range = _range.get_Resize(rowCount, colCount);
_range.set_Value(_optionalValue, values);
}
/// <summary>
/// 创建一个Excel程序实例
/// </summary>
private void CreateExcelRef()
{
_excelApp = new Excel.Application();
_books = (Excel.Workbooks)_excelApp.Workbooks;
_book = (Excel._Workbook)(_books.Add(_optionalValue));
_sheets = (Excel.Sheets)_book.Worksheets;
_sheet = (Excel._Worksheet)(_sheets.get_Item(1));
}
}
}
ExcelHelper Excel,Export,Import的更多相关文章
- 【从翻译mos文章】不再用par file如果是,export or import 包含大写和小写表名称表
不再用par file如果是,export or import 包含大写和小写表名称表 参考原始: How to Export or Import Case Sensitive Tables With ...
- es6环境中,export与import使用方法
前言 参考自阮一峰大神的教程:http://es6.ruanyifeng.com/?search=export&x=6&y=5#docs/module#export-命令 声明:如有问 ...
- export,export default和import的区别以及用法
首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和import export用于对外输出本模块(一个文件可以理解为一个模块)变量的接口 ...
- export ,export default 和 import 区别 以及用法
首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...
- export,import ,export default区别
export,import ,export default区别 一.export,import ,export default ES6模块主要有两个功能:export和import export用于对 ...
- export,import ,export default 彻底弄痛
ES6模块主要有两个功能:export和import 说白了就是一个淡出一个导入,就相当于以前的公共js样,哪个页面要用,就script 引入这个js ,然后 无耻的调用这个js中的方法了. ex ...
- export,import ,export default是什么
首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...
- [ES6]import 与export的用法 ,export 与export default 的 区别 以及用法
一.import 与export export(导出):用于对外输出本模块(一个文件可以理解为一个模块)变量的接口: import(导入):用于在一个模块中加载另一个含有export接口的模块. 1. ...
- export,import ,export default的区别
首先要知道export,import ,export default是什么 ES6模块主要有两个功能:export和importexport用于对外输出本模块(一个文件可以理解为一个模块)变量的接口i ...
随机推荐
- script:查看redo产生的历史信息
script:查看redo产生的历史信息 SQL> alter session set nls_date_format='dd-mon-yy'; SQL> set lines 160 p ...
- hadoop源码下载地址
http://svn.apache.org/repos/asf/hadoop/common/branches/
- Python天天美味(25) - 深入理解yield
Python天天美味(25) - 深入理解yield - CoderZh - 博客园 Python天天美味(25) - 深入理解yield yield的英文单词意思是生产,刚接触Python的时候 ...
- c coding style之学习篇
1. 使用do-while结构去避免潜在的内存泄漏问题. do { p1 = malloc(10); if (null == p1) { break; ...
- 让自己的软件实现拖拽打开文件(使用WM_DROPFILES消息和DragQueryFile函数)
//声明 protected procedure WMDROPFILES(var Msg : TMessage); message WM_DROPFILES; //实现 procedure TForm ...
- 得到一个临时的文件名称(使用GetTempFileName API函数)
function GetExePath: string; begin Result := ExtractFilePath(ParamStr()); end; function GetTempFileN ...
- ThinkPHP页面跳转、Ajax技巧详细介绍(十八)
原文:ThinkPHP页面跳转.Ajax技巧详细介绍(十八) ThinkPHP页面跳转.Ajax技巧详细介绍 一.页面跳转 $this->success('查询成功',U('User/test' ...
- 记录break和continue的区别
我对break 和 continue 还是有点搞不清除,今天在看<Thinking in Java>,看到这个,学习了一下. break的作用是跳出这个循环(如果这个break ...
- graphterm 0.40.1 : Python Package Index
graphterm 0.40.1 : Python Package Index graphterm 0.40.1 Downloads ↓ A Graphical Terminal Interface ...
- OpenCV-Python教程(5、初级滤波内容)
本篇文章介绍如何用OpenCV-Python来实现初级滤波功能. 提示: 转载请详细注明原作者及出处,谢谢! 本文介绍使用OpenCV-Python实现基本的滤波处理 本文不介绍滤波处理的详细概念,所 ...