如何将数据库数据保存到不同的excel中 [英] How to save database data into different excel

查看:97
本文介绍了如何将数据库数据保存到不同的excel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表记录中如下





In my table record as follows


ASSociateID   Project ID  ProjectName   Area   ShiftType   Shiftdate

  2456          1           A            BLR      IN         28 jun 2016
  3424          2           B            BLR      IN         30 Jun 2016
  3422          3           C            BLR      IN          1 Aug 2016
  3342          4           D            BLR      IN          2 Aug 2016

  5534          1           A            KOC      OUT         29 Jun 2016
  3353          2           B            KOC      OUT         30 jun 2016
  4343          3           C            KOC      OUT          1 Aug 2016
  4535          4           D            KOC      OUT          2 Aug 2016





我的控制台应用程序代码如下



使用控制台应用程序将上述数据库数据显示到excel





my console application code as follows

Displaying above database data into excel using console application

string connectionstring = "Server=(local);initial           catalog=OneC;Trusted_Connection=True";
      SqlConnection con = new SqlConnection(connectionstring);
      SqlCommand command= new SqlCommand();
      SqlDataReader dr;
      DataSet ds= new DataSet();
      command.CommandText = "Select * from  Employee";
      command.CommandType = CommandType.Text;
      command.Connection = con;
      con.Open();
      dr= cmd.ExecuteReader();
       if (dr.HasRows)
       {
 
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\Details\Excel.xls"))
        {
           while (dr.Read())
               {
                  for (int i = 0; i < dr.FieldCount; i++)
                   {
                        sw.Autoflush = true;
                        sw.write(dr[i].Tostring() +"\t");
                   }
                         sw,writeline("\n");
               }
        }
                          con.Close();
       }





当我在一个excel数据中的c文件夹中的sysem执行上面的代码时保存



i想要保存上面的数据ShiftType(IN)保存在一个excel中



我想要上面的要保存的数据shiftType(Out)要保存在另一个excel中



对于我上面的代码更改,我必须做的是将IN和OUT保存在不同的excel中



我尝试过:



表格数据库数据将被保存在两个不同的excel中。



当我执行时我想要输出如下



我想要的一个excel保存如下





when i execute the above code in my sysem in c folder in one excel data are saved

i want the above data to be saved ShiftType (IN) to be saved in one excel

and i want the above data to be saved shiftType (Out) to be saved in another excel

For that in my above code changes i have to do to save IN and OUT in different excel

What I have tried:

Form database data are to be saved in two different excel.

When i execute i want output as follows

In one excel i want to save as follows

ASSociateID   Project ID  ProjectName   Area   ShiftType   Shiftdate

   2456          1           A            BLR      IN         28 jun 2016
   3424          2           B            BLR      IN         30 Jun 2016
   3422          3           C            BLR      IN          1 Aug 2016
   3342          4           D            BLR      IN          2 Aug 2016









另一个excel我想保存如下







In another excel i want to save as follows

ASSociateID   Project ID  ProjectName   Area   ShiftType   Shiftdate
   5534          1           A            KOC      OUT         29 Jun 2016
   3353          2           B            KOC      OUT         30 jun 2016
   4343          3           C            KOC      OUT          1 Aug 2016
   4535          4           D            KOC      OUT          2 Aug 2016

推荐答案

我昨天在这个问题的原始帖子 [ ^ ]。请不要重新发布相同的问题。如果您有要添加的信息,请编辑您的原件。



我还解释说您的代码创建Excel文件,它创建简单文本文件。
I answered this yesterday in your original post of this question[^]. Please do not repost the same question. If you have information to add then edit your original.

I also explained that your code is not creating Excel files, it is creating simple text files.


你的代码有点令人困惑,但我会忽略大部分的怪异。



你会想要的两个StreamWriters,每个记录类型一个,然后使用ShiftType的值选择要写入的StreamWriter。
Your code is a bit confusing, but I'll ignore most of the weirdness.

You will want two StreamWriters, one for each type of record, then use the value of ShiftType to select the StreamWriter to write to.


引用:

我希望保存上面的数据ShiftType(IN)保存在一个excel中

我想要保存上面的数据shiftType(Out)保存在另一个excel

i want the above data to be saved ShiftType (IN) to be saved in one excel
and i want the above data to be saved shiftType (Out) to be saved in another excel

你只需创建2 StreamWriter 对象,并在每个 StreamWriter 中写下你想要的内容。 br />


注意:您没有创建Excel文件,而是创建带有制表符分隔数据的文本文件。

you just have to create 2 StreamWriter objects and write what you want in each StreamWriter.

Nota: you are not creating Excel files, you are creating text file with tab separated data.


这篇关于如何将数据库数据保存到不同的excel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆