nopi导入导出
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel; namespace PDM {
public partial class Export : Form {
public Export() {
InitializeComponent();
this.tbxserver.Text = "192.168.17.216";
this.tbxserverName.Text = "mf_spare_2014_0801";
this.tbxname.Text = "sa";
this.tbxpassword .Text= "sunlight";
} private void button1_Click(object sender, EventArgs e) {
try {
string strsql;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "d:\\";
openFileDialog.Filter = "文本文件|*.*|C#文件|*.cs|所有文件|*.*";
openFileDialog.FilterIndex = 1;
DataSet dss = new DataSet();
SqlConnection oraCon = new SqlConnection(string.Format("server={0};database={1};uid={2};pwd={3}", this.tbxserver.Text, this.tbxserverName.Text,this.tbxname.Text, this.tbxpassword.Text));
oraCon.Open();
// SqlConnection oraCon = new SqlConnection(string.Format("user id={0};data source={1};password={2}", this.tbxname.Text, this.tbxserver.Text, this.tbxpassword.Text));
if(openFileDialog.ShowDialog() == DialogResult.OK) {
var fName = openFileDialog.FileName;
var importtable = XlSToDataTable(fName, "dd", 0);
this.dataGridView1.DataSource = importtable;
foreach(DataRow item in importtable.Rows) {
DataTable dt = new DataTable();
dt.TableName = item[0].ToString();
strsql = (item[1].ToString());
SqlDataAdapter oraDap = new SqlDataAdapter(strsql, oraCon);
oraDap.Fill(dt);
dss.Tables.Add(dt);
}
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
saveFileDialog1.FileName = "导出数据";
saveFileDialog1.FilterIndex = 1;
if(saveFileDialog1.ShowDialog() == DialogResult.OK) {
GridToExcelByNPOI(dss, saveFileDialog1.FileName);
}
} oraCon.Close();
} catch(Exception ex) {
throw ex;
}
} private static void GridToExcelByNPOI(DataSet ds, string strExcelFileName) {
try {
HSSFWorkbook workbook = new HSSFWorkbook();
int i = 0;
foreach(DataTable dt in ds.Tables) {
i++;
ISheet sheet = workbook.CreateSheet(dt.TableName);
//用column name 作为列名
int icolIndex = 0;
IRow headerRow = sheet.CreateRow(0);
foreach(DataColumn item in dt.Columns) {
ICell cell = headerRow.CreateCell(icolIndex);
cell.SetCellValue(item.ColumnName);
icolIndex++;
}
int iRowIndex = 1;
int iCellIndex = 0;
foreach(DataRow Rowitem in dt.Rows) {
IRow DataRow = sheet.CreateRow(iRowIndex);
foreach(DataColumn Colitem in dt.Columns) {
ICell cell = DataRow.CreateCell(iCellIndex);
cell.SetCellValue(Rowitem[Colitem].ToString());
iCellIndex++;
}
iCellIndex = 0;
iRowIndex++;
}
}
FileStream file = new FileStream(strExcelFileName, FileMode.OpenOrCreate);
workbook.Write(file);
file.Flush();
file.Close();
} catch(Exception ex) {
} finally {
//workbook = null;
}
} /// <summary>
/// Excel文件导成Datatable
/// </summary>
/// <param name="strFilePath">Excel文件目录地址</param>
/// <param name="strTableName">Datatable表名</param>
/// <param name="iSheetIndex">Excel sheet index</param>
/// <returns></returns>
public static DataTable XlSToDataTable(string strFilePath, string strTableName, int iSheetIndex) { string strExtName = Path.GetExtension(strFilePath); DataTable dt = new DataTable();
if(!string.IsNullOrEmpty(strTableName)) {
dt.TableName = strTableName;
} if(strExtName.Equals(".xls") || strExtName.Equals(".xlsx")) {
using(FileStream file = new FileStream(strFilePath, FileMode.Open, FileAccess.Read)) {
HSSFWorkbook workbook = new HSSFWorkbook(file);
ISheet sheet = workbook.GetSheetAt(iSheetIndex); //列头
foreach(ICell item in sheet.GetRow(sheet.FirstRowNum).Cells) {
dt.Columns.Add(item.ToString(), typeof(string));
} //写入内容
System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
while(rows.MoveNext()) {
IRow row = (HSSFRow)rows.Current;
if(row.RowNum == sheet.FirstRowNum) {
continue;
} DataRow dr = dt.NewRow();
foreach(ICell item in row.Cells) {
switch(item.CellType) {
case CellType.Boolean:
dr[item.ColumnIndex] = item.BooleanCellValue;
break;
case CellType.Formula:
switch(item.CachedFormulaResultType) {
case CellType.Boolean:
dr[item.ColumnIndex] = item.BooleanCellValue;
break;
case CellType.Numeric:
if(DateUtil.IsCellDateFormatted(item)) {
dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
} else {
dr[item.ColumnIndex] = item.NumericCellValue;
}
break;
case CellType.String:
string str = item.StringCellValue;
if(!string.IsNullOrEmpty(str)) {
dr[item.ColumnIndex] = str.ToString();
} else {
dr[item.ColumnIndex] = null;
}
break;
case CellType.Unknown:
case CellType.Blank:
default:
dr[item.ColumnIndex] = string.Empty;
break;
}
break;
case CellType.Numeric:
if(DateUtil.IsCellDateFormatted(item)) {
dr[item.ColumnIndex] = item.DateCellValue.ToString("yyyy-MM-dd hh:MM:ss");
} else {
dr[item.ColumnIndex] = item.NumericCellValue;
}
break;
case CellType.String:
string strValue = item.StringCellValue;
if(!string.IsNullOrEmpty(strValue)) {
dr[item.ColumnIndex] = strValue.ToString();
} else {
dr[item.ColumnIndex] = null;
}
break;
case CellType.Unknown:
case CellType.Blank:
default:
dr[item.ColumnIndex] = string.Empty;
break;
}
}
dt.Rows.Add(dr);
}
}
}
return dt;
} private void btexport_Click(object sender, EventArgs e) { }
}
}
nopi导入导出的更多相关文章
- NOPI导入导出EXCEL
一.简介 1. 什么是NPOI NPOI,顾名思义,就是POI的.NET版本.那POI又是什么呢?POI是一套用Java写成的库,能够帮助开发者在没有安装微软Office的情况下读写Office 97 ...
- NOPI实现导入导出泛型List,支持自定义列
概述 业务上需要自定义列的Excel的导入导出,在网上看了好多资料,很多都是有Bug而且都是支持Excel和DataTable的转换,所以自己总结了一下,应用.NET平台上的NPOI封装了支持自定义列 ...
- asp.net mvc4 easyui datagrid 增删改查分页 导出 先上传后导入 NPOI批量导入 导出EXCEL
效果图 数据库代码 create database CardManage use CardManage create table CardManage ( ID ,) primary key, use ...
- ASP.NET Core使用EPPlus导入导出Excel
开发过程中,经常会遇到导入导出数据的需求,本篇博客介绍在.NET Core中如何使用EPPlus组件导入导出Excel EPPlus: EPPlus是使用Open Office XML格式(xlsx) ...
- 企业级自定义表单引擎解决方案(十六)--Excel导入导出
Excel对于后端管理系统来说,永远都是绕不开的话题,开发Excel导入导出功能往往都比较麻烦,因为涉及到Excel导入模板制作.Excel表格数据与系统数据库表字段映射.Excel导入数据验证.验证 ...
- CRL快速开发框架系列教程九(导入/导出数据)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- ITTC数据挖掘平台介绍(五) 数据导入导出向导和报告生成
一. 前言 经过了一个多月的努力,软件系统又添加了不少新功能.这些功能包括非常实用的数据导入导出,对触摸进行优化的画布和画笔工具,以及对一些智能分析的报告生成模块等.进一步加强了平台系统级的功能. 马 ...
- ASP.NET Core 导入导出Excel xlsx 文件
ASP.NET Core 使用EPPlus.Core导入导出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件导入导出,可以运行在Windows, Li ...
- Oracle 数据库导入导出 dmp文件
转自: http://hi.baidu.com/ooofcu/blog/item/ec5d1f9580d41f007af48077.html 首先询问对方数据库的表空间名称和大小,然后在你的oracl ...
随机推荐
- Nginx+Varnish又开始新的征程了
要自己多测试一下.总觉得机器不够用.
- Android 隐式意图 让用户选择一个浏览器访问网址
Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setData(Uri.parse("h ...
- xstream 别名的用法<转>
1.xstream的alias使用方法: 1.1 作用:将序列化中的类全量名称,用别名替换. 1.2 使用方法:xstream.alias("blog", Blog.class) ...
- unity3d AI's sight
just finished -----by wolf96
- 【转】分析器窗口 Profiler window
转自unity圣典: http://game.ceeger.com/Manual/ProfilerWindow.html http://game.ceeger.com/Manual/Profiler. ...
- HDOJ/HDU 2710 Max Factor(素数快速筛选~)
Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 < ...
- Supervisord管理
原文地址:http://blog.csdn.net/fyh2003/article/details/6837970 学习笔记 Supervisord可以通过sudo easy_install supe ...
- 使用AppDelegate单例,解决子视图无法给父视图发送消息的问题
关于单例模式,我会在实验过后再开一个博客重点讲单例的使用,这里只是介绍我在PhotoForBingyan的照片滤镜的项目中使用AppDelegate单例的情况. 碰到的问题: 由于这个项目是个多视图的 ...
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- [转]Ubuntu上的包管理:dpkg,apt和aptitude
一直以来对于ubuntu的包管理的概念就是apt-get,偶尔手动装个包就是dpkg -i,现在觉得是要系统地了解一下这几个包管理的命令. 原文转自: http://zhouliang.pro/201 ...