简体   繁体   English

数据以特定格式导入Excel文件

[英]Data into excel file in a particular format

Hi I am getting the data from Sql Server like 嗨,我正在从Sql Server获取数据,例如

在此处输入图片说明

for about 1000 stores. 约有1000家商店。

I need to put data in to excel file like as 我需要像这样将数据放入excel文件

在此处输入图片说明

and so on until now i have managed to code like as console application 依此类推,直到现在我已经设法像控制台应用程序一样进行编码

if (reader.HasRows) {
    while (reader.Read()) {
        SqlDataAdapter da = new SqlDataAdapter("StoresWithProduct", connection);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        da.SelectCommand.Parameters.AddWithValue("@StoreID", reader.GetInt32(0));
        DataSet ds = new DataSet();
        da.Fill(ds, "table1");
        foreach (DataRow dr1 in ds.Tables["table1"].Rows) {
            Console.Write("Store Code:" + dr1["StoreCode"] + "\t");
            foreach (DataRow dr in ds.Tables["table1"].Rows)
            {
                Console.Write(dr["itemNumber"]+"-" + dr["quantity"]+"\t\n");
            }
            break;
        }
    }
} else {
    Console.WriteLine("No rows found.");
}
Console.ReadLine();
reader.Close();
connection.Close();

but not able to put into the excel file the way i want, any pointer will be appriciated. 但是无法以我想要的方式放入excel文件,任何指针都将被使用。

Thanks 谢谢

What you are doing should work if you wrote the output to a text file. 如果将输出写入文本文件,那么您正在做的事情应该可以工作。 Using CSV ( wikipedia ) you can easily export your data to Excel. 使用CSV( Wikipedia ),您可以轻松地将数据导出到Excel。 See this link , due to the lack of standard, using tabs like you do should work. 由于缺乏标准,请参见此链接 ,使用类似的tabs应该可以正常工作。

You simply need to output the data row by row, using a separator (usually , , ; or tab ) and use a new line to define a new row. 你只需要输出的数据一行接一行,使用分离器(通常,;tab ),并使用一个new line定义一个新列。

Excel supports CSV files as Text Files . Excel支持CSV文件作为Text Files I don't have Excel on this PC, but usually there's an import wizard to help you with the creation of columns. 我在这台PC上没有Excel,但是通常会有一个导入向导来帮助您创建列。

I would recommend looking at EPPlus 我建议看一下EPPlus

We've used this library extensively for generating complex Excel workbooks including pivot tables. 我们已广泛使用此库来生成复杂的Excel工作簿,包括数据透视表。 Usage is very straightforward with plenty of examples and help on codeplex site. 用法非常简单,有大量示例,并在Codeplex网站上提供了帮助。

I hope this helps! 我希望这有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM