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 ...
随机推荐
- Windows窗口样式速查参考,Delphi窗口控件的风格都有它们来决定(附Delphi何时用到它们,并举例说明)good
/* 窗口样式参考列表(都是GetWindowLong的GWL_STYLE风格,都是TCreateParams.Sytle的一部分),详细列表如下:https://msdn.microsoft.com ...
- 《深度探索c++对象模型》chapter3 Data语意学
一个空的class:如 class X{} ; sizeof(X)==1; sizeof为什么为1,他有一个隐晦的1 byte,那是被编译器安插进去的一个char,这使得class2的两个object ...
- Android Studio启动时Fetching android sdk component information超时的解决方案
1)进入刚安装的Android Studio目录下的bin目录.找到idea.properties文件,用文本编辑器打开. 2)在idea.properties文件末尾添加一行: disable.an ...
- Linux Eclipse代码提示功能设置(Java & C/C++)
最近在Linux下开发,由于长期使用Visual Studio 2010,对代码提示功能情有独钟,现在在Linux下,使用Eclipse做开发,当然免不了怀念Visual Studio强悍的代码提示, ...
- git入门超详细(转载)
转自:http://www.cnblogs.com/tugenhua0707/p/4050072.html Git使用教程 一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SV ...
- 网络编程——TCP协议的三次握手和四次挥手
三次握手原理解析 TCP握手协议在TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接. 第一次握手:建立连接时,客户端发送syn包(syn=j)到服务器,并进入SYN_SEND ...
- Java IO复习(一)
package com.zyw.file; import java.io.*; /** * Created by zyw on 2016/3/10. */ public class IOTest { ...
- flexpaper 与js 交互
flash 代码//写到要响应的方法体中import flash.external.ExternalInterface;ExternalInterface.call("alert" ...
- Magic Pairs - SGU 119(同余)
题目大意:如果A0*X + B0*Y能够整除 N,求出来多有少A*X+B*Y 也能够整除去N,求出所有的A,B(0<=A,B<N) 分析:有条件可以知道 A*X+B*Y = K *(A0* ...
- Pipe - POJ 1039(线段相交交点)
题目大意:有一个不反光并且不透光的管道,现在有一束光线从最左端进入,问能达到的最右端是多少,输出x坐标. 分析:刚开始做是直接枚举两个点然后和管道进行相交查询,不过这样做需要考虑的太多,细节不容易 ...