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 ...
随机推荐
- 修改表增加字段默认值default
对个生产库的表增加1个字段.字段类型是INT型, 表数据有2千万条, alter table table_name add xxoo number(4) default 0 ; 因此 不仅要修改字典 ...
- 【HDOJ】1422 重温世界杯
简单题. #include <stdio.h> #define MAXN 100005 int wi[MAXN], li[MAXN]; ]; int main() { int n, tot ...
- 导航软件 CH Round #57 - Story of the OI Class
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2357%20-%20Story%20of%20the%20OI%20Class/导航软件 题解:刚开始看见题目, ...
- 【EJS】
// 用=号输出,就会被escapge转义编码 <%= VARIABLE_NAME %> // 用“-”输出原始内容, 不会被escape <%- VARIABLE_NAME %&g ...
- iso学习网站记录
[零基础学习iOS开发] http://www.cnblogs.com/mjios/archive/2013/04/24/3039357.html 非零基础学习iOS开发2-Objective-C h ...
- 网络流(最大流)CodeForces 512C:Fox And Dinner
Fox Ciel is participating in a party in Prime Kingdom. There are n foxes there (include Fox Ciel). T ...
- Linux学习笔记15——GDB 命令详细解释【转】
GDB 命令详细解释 Linux中包含有一个很有用的调试工具--gdb(GNU Debuger),它可以用来调试C和C++程序,功能不亚于Windows下的许多图形界面的调试工具. 和所有常用的调试工 ...
- HDOJ 1716 排列2(next_permutation函数)
Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡 ...
- this用法
this是js的一个关键字,随着函数使用场合不同,this的值会发生变化.但是总有一个原则,那就是this指的是调用函数的那个对象. 1.纯粹函数调用. function test() { this. ...
- 不使用OCI8接口如何连接PHP和Oracle
随着网站规模的扩大,MySql显然不能满足需求,在许多网站都 采用大型数据库Oracle的情况下,如何使用PHP来访问Oracle变的越发重要了. 我从我编写的一个简单iERP系统谈我自己是如何做 ...