最近项目要求实现数据库之间数据在各个数据库之间导入导出,在此做个笔记

1. 将Oracle中的表导入到Excel中,反之亦然

   private static readonly string connectionString = ConfigurationManager.ConnectionStrings["OracleConnection"].ConnectionString;
1 public void print(DataGridView dataGridView1)
{
//导出到execl
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "导出Excel2003~2007 (*.xls)|*.xls|导出Excel2010~2013 (*.xlsx)|*.xlsx";
saveFileDialog.FilterIndex = ;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
saveFileDialog.Title = "导出文件保存路径";
saveFileDialog.ShowDialog();
string strName = saveFileDialog.FileName;
if (strName.Length != )
{
//没有数据的话就不往下执行
if (dataGridView1.Rows.Count == )
return; // toolStripProgressBar1.Visible = true;
System.Reflection.Missing miss = System.Reflection.Missing.Value;
//实例化一个Excel.Application对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
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 = ;
//生成Excel中列头名称
for (int i = ; i < dataGridView1.Columns.Count; i++)
{
excel.Cells[, i + ] = dataGridView1.Columns[i].HeaderText;//输出DataGridView列头名
}
//把DataGridView当前页的数据保存在Excel中
for (int i = ; i < dataGridView1.Rows.Count - ; i++)
{
for (int j = ; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1[j, i].ValueType == typeof(string))
{
excel.Cells[i + , j + ] = "'" + dataGridView1[j, i].Value.ToString();
}
else
{
excel.Cells[i + , j + ] = dataGridView1[j, i].Value.ToString();
}
}
}
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, "错误提示");
}
}
    public void printAll(System.Data.DataTable dt)
{
//导出到execl
try
{
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 != )
{
//没有数据的话就不往下执行
if (dt.Rows.Count == )
return; // toolStripProgressBar1.Visible = true;
System.Reflection.Missing miss = System.Reflection.Missing.Value;
//实例化一个Excel.Application对象
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
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";
//生成Excel中列头名称
for (int i = ; i < dt.Columns.Count; i++)
{
excel.Cells[, i + ] = dataGridView1.Columns[i].HeaderText;//输出DataGridView列头名
} //把DataGridView当前页的数据保存在Excel中
if (dt.Rows.Count > )
{
for (int i = ; i < dt.Rows.Count; i++)//控制Excel中行,上下的距离,就是可以到Excel最下的行数,比数据长了报错,比数据短了会显示不完
{
for (int j = ; j < dt.Columns.Count; j++)//控制Excel中列,左右的距离,就是可以到Excel最右的列数,比数据长了报错,比数据短了会显示不完
{
string str = dt.Rows[i][j].ToString();
excel.Cells[i + , j + ] = "'" + str;//i控制行,从Excel中第2行开始输出第一行数据,j控制列,从Excel中第1列输出第1列数据,"'" +是以string形式保存,所以遇到数字不会转成16进制
}
}
}
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, "错误提示");
}
}

这两个方法都可以将Oracle导出到Excel中。转自http://www.cnblogs.com/cpcpc/archive/2012/11/13/2767934.html

 class Excel2Oracle
{
private static readonly string connectionString = ConfigurationManager.ConnectionStrings["OracleConnection"].ConnectionString;
public void InsertData2Oracle(DataSet ds)
{
using (OleDbConnection oraCon = new OleDbConnection(connectionString))
{
oraCon.Open();
using (OleDbCommand cmd = new OleDbCommand())
{
cmd.Connection = oraCon;
cmd.CommandType = CommandType.Text; for (int i = ; i < ds.Tables[].Rows.Count; i++)
{
List<string> list_ = new List<string>();
for (int j = ; j < ds.Tables[].Columns.Count; j++)
{
list_.Add(ds.Tables[].Rows[i][j].ToString());
}
string temp = "";
string t = ","; for (int k = ; k < list_.Count; k++)
{
if (k == list_.Count - ) t = "";
temp = temp + "'" + list_[k] + "'" + t + "";
cmd.CommandText = string.Format("Insert into TBUSER values(" + temp + ")");
} cmd.ExecuteNonQuery();
} }
oraCon.Close(); }
}
    }  

代码比废话更要迷人,我就不多说什么了

C# 实现Oracle中的数据与Excel之间的转换的更多相关文章

  1. Oracle中的数据类型和数据类型之间的转换

    Oracle中的数据类型 /* ORACLE 中的数据类型: char 长度固定 范围:1-2000 VARCHAR2 长度可变 范围:1-4000 LONG 长度可变 最大的范围2gb 长字符类型 ...

  2. 将Oracle数据库中的数据写入Excel

    将Oracle数据库中的数据写入Excel 1.准备工作 Oracle数据库"TBYZB_FIELD_PRESSURE"表中数据如图: Excel模板(201512.xls): 2 ...

  3. C#读取Excel表格数据到DataGridView中和导出DataGridView中的数据到Excel

    其实想在datagridview中显示excel表格中的数据跟读取数据库中的数据没什么差别,只不过是创建数据库连接的时候连接字段稍有差别. private void btnShow_Click(obj ...

  4. oracle中的数据对象

    oracle中的数据对象有表.视图.索引.序列等 表的相关操作 1.创建表 方式一: 方式二:create table person( create table person1 id number(1 ...

  5. 在Oracle中更新数据时,抛出:ORA-01008: not all variables bound

    在Oracle中更新数据时,抛出了一个 :ORA-01008 not all variables bound, 我的理解是不是所有的变量/参数都有边界,不懂: 后来知道了,原来是“不是所有变量/参数都 ...

  6. MyBatis在Oracle中插入数据并返回主键的问题解决

    引言:  在MyBatis中,希望在Oracle中插入数据之时,同一时候返回主键值,而非插入的条数... 环境:MyBatis 3.2 , Oracle. Spring 3.2   SQL Snipp ...

  7. 对.NET中导出数据到EXCEL的几种方法探讨

    最近在做一个报表系统的时候,需要把DATASET中的数据导到EXCEL当中,于是在网上找了一遍,发现了好几种方法,本来以为应该差不多,但后来经过一一试用后,发现在性能上真的差别很大,现在就介绍一下,同 ...

  8. Sqoop_具体总结 使用Sqoop将HDFS/Hive/HBase与MySQL/Oracle中的数据相互导入、导出

    一.使用Sqoop将MySQL中的数据导入到HDFS/Hive/HBase watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWFyb25oYWRvb3A=/ ...

  9. Elasticsearch 2.3.2 从oracle中同步数据

    Elasticsearch 2.3.2 从oracle中同步数据   1         数据批量导入-oracle 采用 elasticsearch-jdbc 插件 安装.版本需要ES版本一致 最新 ...

随机推荐

  1. org.springframework.dao.InvalidDataAccessApiUsageException:The given object has a null identifi的解决方案

    异常信息: org.springframework.dao.InvalidDataAccessApiUsageException: The given object has a null identi ...

  2. BI中PowerDesigner建模

    PowerDesigner下载: http://pan.baidu.com/share/link?shareid=296749&uk=1479845243

  3. codeforces 677B B. Vanya and Food Processor(模拟)

    题目链接: B. Vanya and Food Processor time limit per test 1 second memory limit per test 256 megabytes i ...

  4. hdu-5681 zxa and wifi(dp)

    题目链接: zxa and wifi Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Othe ...

  5. C#右键复制路径

    using System;//Environment using System.Windows.Forms; //add referece of System.Windows.Forms :DataF ...

  6. php学习笔记2--安装apache遇到的问题

    下载apache之后,以管理员身份运行cmd:1.httpd -k install2.httpd -k start出现无法运行的问题,可能的原因是443端口已被占用.在我的机器中是因为安装了VMwar ...

  7. jsp中页面间传汉字参数转码

    转码:a.href="./showCont.jsp?tcontent="+encodeURI(encodeURI(tcontent)); 解码:java.net.URLDecode ...

  8. Linux下DNS服务器的基本搭建

    技术交流群:286866978 安装与配置 1. 装载光驱 2. 卸载光驱 3. 将安装包放在合适的文件夹并解压(有的更换光盘需要重新装载) 4. 安装 5. 重定向配置文件 6. 配置named.c ...

  9. RedHat Install

    1. 插入光盘1并从光盘启动加载镜像文件 2. 回车后进入安装流程 3. 选择语言 4. 选择键盘布局 5. 鼠标配置 6. 选择安装类型 7. 选择分区类型 8. 添加一个boot分区 9. 新建一 ...

  10. JQuery之append和appendTo的区别,还有js中的appendChild用法

    JQuery之append和appendTo的区别 append()前面是要选择的对象,后面是要在对象内插入的元素内容 appendTo()前面是要插入的元素内容且为Jquery对象,而后面是要选择的 ...