如何将数据库中的数据导出到excel

如何将数据库中的数据导出到excel,第1张

以下都只是介绍 *** 作的原理,具体要求要在应用中具体分析改变。

1 此方法常用在form或者Console Application中,使用时须用要添加Reference,具体做法:

右键点击项目添加“Add Reference”,在Tom组件下,选择“Microsoft Excel 140 Object Library”,然后在项目中使用

下面注释//it looks like excele table start with 1 not 1

应该为//it looks like excele table start with 1 not 0

[csharp] view plaincopy

private static void exportToExcel(DataTable dt)

{

ExcelApplication excel=new ExcelApplication();

excelApplicationWorkbooksAdd(true);

excelVisible = true;

//get the columns

for (int i = 0; i < dtColumnsCount;i++ )

{

//here is started with 1

//it looks like excele table start with 1 not 1

excelCells[1, i + 1] = dtColumns[i]ColumnNameToString();

}

//get the data in rows

for (int row = 0; row < dtRowsCount;row++ )

{

for (int col = 0; col < dtColumnsCount; col++)

{

excelCells[row+2, col+1] = dtRows[row][dtColumns[col]]ToString();

}

}

//FolderBrowserDialog path = new FolderBrowserDialog();//打开文件对话框

//pathShowDialog();

//textBox1Text = pathSelectedPath;//选择文件夹

//save excel

//excelSaveWorkspace();

excelQuit();

}

2 在web应用中,可通过>

column1 datatype [not null] [not null primary key], column2 datatype [not null],)

说明:

datatype --是资料的格式,详见表。

nut null --可不可以允许资料有空的(尚未有资料填入)。

primary key --是本表的主键。

2、更改表格

alter table table_name add column column_name datatype

说明:增加一个栏位(没有删除某个栏位的语法。)

lter table table_name add primary key (column_name)

说明:更改表得的定义把某个栏位设为主键。

alter table table_name drop primary key (column_name)

说明:把主键的定义删除。

3、建立索引

create index index_name on table_name (column_name)

说明:对某个表格的栏位建立索引以增加查询时的速度。

4、删除

drop table_name

drop index_name

二、资料形态 datatypes

smallint

16 位元的整数。

interger

32 位元的整数。

decimal(p,s)

p 精确值和 s 大小的十进位整数,精确值p是指全部有几个数(digits)大小值,s是指小数后有几位数。如果没有特别指定,则系统会设为 p=5; s=0 。

float

32位元的实数。

double

64位元的实数。

char(n)

n 长度的字串,n不能超过 254。

varchar(n)

长度不固定且其最大长度为 n 的字串,n不能超过 4000。

graphic(n)

和 char(n) 一样,不过其单位是两个字元 double-bytes, n不能超过127。这个形态是为支援两个字元长度的字体,例如中文字。

vargraphic(n)

可变长度且其最大长度为 n 的双字元字串,n不能超过 2000。

date

包含了 年份、月份、日期。

time

包含了 小时、分钟、秒。

timestamp

包含了 年、月、日、时、分、秒、千分之一秒。

三、资料 *** 作 dml(data manipulation language)

资料定义好之后接下来的就是资料的 *** 作。资料的 *** 作不外乎增加资料(insert)、查询资料(query)、更改资料(update) 、删除资料(delete)四种模式,以下分 别介绍他们的语法:

1、增加资料:

insert into table_name (column1,column2,) values ( value1,value2, )

说明:

1若没有指定column 系统则会按表格内的栏位顺序填入资料。

2栏位的资料形态和所填入的资料必须吻合。

3table_name 也可以是景观 view_name。

insert into table_name (column1,column2,) select columnx,columny, from another_table

说明:也可以经过一个子查询(subquery)把别的表格的资料填入。

2、查询资料:

基本查询

select column1,columns2, from table_name

说明:把table_name 的特定栏位资料全部列出来

select from table_name where column1 = xxx [and column2 > yyy] [or column3 <> zzz]

说明:

1''表示全部的栏位都列出来。

2where 之后是接条件式,把符合条件的资料列出来。

select column1,column2 from table_name order by column2 [desc]

说明:order by 是指定以某个栏位做排序,[desc]是指从大到小排列,若没有指明,则是从小到大排列

组合查询

组合查询是指所查询得资料来源并不只有单一的表格,而是联合一个以上的表格才能够得到结果的。

select from table1,table2 where table1colum1=table2column1

说明:

1查询两个表格中其中 column1 值相同的资料。

2当然两个表格相互比较的栏位,其资料形态必须相同。

3一个复杂的查询其动用到的表格可能会很多个。

整合性的查询:

select count () from table_name where column_name = xxx

说明:

查询符合条件的资料共有几笔。

select sum(column1) from table_name

说明:

1计算出总和,所选的栏位必须是可数的数字形态。

2除此以外还有 avg() 是计算平均、max()、min()计算最大最小值的整合性查询。

select column1,avg(column2) from table_name group by column1 having avg(column2) > xxx

说明:

1group by: 以column1 为一组计算 column2 的平均值必须和 avg、sum等整合性查询的关键字一起使用。

2having : 必须和 group by 一起使用作为整合性的限制。

复合性的查询

select from table_name1 where exists ( select from table_name2 where conditions )

说明:

1where 的 conditions 可以是另外一个的 query。

2exists 在此是指存在与否。

select from table_name1 where column1 in ( select column1 from table_name2 where conditions )

说明:

1 in 后面接的是一个集合,表示column1 存在集合里面。

2 select 出来的资料形态必须符合 column1。

其他查询

select from table_name1 where column1 like 'x%'

说明:like 必须和后面的'x%' 相呼应表示以 x为开头的字串。

select from table_name1 where column1 in ('xxx','yyy',)

说明:in 后面接的是一个集合,表示column1 存在集合里面。

select from table_name1 where column1 between xx and yy

说明:between 表示 column1 的值介于 xx 和 yy 之间。

3、更改资料:

update table_name set column1='xxx' where conditoins

说明:

1更改某个栏位设定其值为'xxx'。

2conditions 是所要符合的条件、若没有 where 则整个 table 的那个栏位都会全部被更改。

4、删除资料:

delete from table_name where conditions

说明:删除符合条件的资料。

说明:关于where条件后面如果包含有日期的比较,不同数据库有不同的表达式。具体如下:

(1)如果是access数据库,则为:where mydate>#2000-01-01#

(2)如果是oracle数据库,则为:where mydate>cast('2000-01-01' as date) 或:where mydate>to_date('2000-01-01','yyyy-mm-dd')

在delphi中写成:

thedate='2000-01-01';

query1sqladd('select from abc where mydate>cast('+''''+thedate+''''+' as date)');

如果比较日期时间型,则为:

where mydatetime>to_date('2000-01-01 10:00:01','yyyy-mm-dd hh24:mi:ss')

不清楚,你怎么建的表,是使用脚本,还是直接用菜单

用简单脚本试试,试试用菜单建的有问题

create table test

colum1 char(1)

, colum2 char(1)

以上就是关于如何将数据库中的数据导出到excel全部的内容,包括:如何将数据库中的数据导出到excel、如何在数据库中添加新列、sql 表结构等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9344194.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-27
下一篇 2023-04-27

发表评论

登录后才能评论

评论列表(0条)

保存