从列表导出到c#中的excel(xls或xlsx)文件 [英] Export from a list to excel(xls or xlsx) file in c#

查看:217
本文介绍了从列表导出到c#中的excel(xls或xlsx)文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助从列表中传输数据< int>到只有一列的xls。谁能帮帮我吗?谢谢。

I want help in transferring data from a list<int> to an xls which has just one column. Can anyone please help me? Thank You.

推荐答案

你好;



希望有所帮助:



hello;

hope that will help :

<pre lang="c#">Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();

xla.Visible = true;

Microsoft.Office.Interop.Excel.Workbook wb = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);

Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)xla.ActiveSheet;

int i = 1;

int j = 1;

foreach (ListViewItem comp in listView1.Items)

{

ws.Cells[i, j] = comp.Text.ToString();

//MessageBox.Show(comp.Text.ToString());

foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)

{

ws.Cells[i, j] = drv.Text.ToString();

j++;

}

j = 1;

i++;

}





问候......



regards...


你可以治疗一个xls文件作为数据库表(工作簿是数据库,工作表是表)

并使用System.Data.Oledb中的类写入它。可以在 http://www.connectionstrings.com/excel [ ^ ]



在您的情况下,最简单解决方案可能只是将您的数据写为文本,每行一个项目,并为输出文件提供.csv扩展名。在excel中打开文本文件会自动转换它,然后你可以另存为xls或xlsx。
You can treat an xls file as a database table (the workbook is the database and the worksheet is the table)
and write to it using the classes in System.Data.Oledb. The database connectionstring to use can be found at http://www.connectionstrings.com/excel[^]

In your case, the simplest solution might be just to write your data as text, one item per line, and give the output file a .csv extension. Opening the text file in excel will automatically convert it , then you can "save as" xls or xlsx.


这篇关于从列表导出到c#中的excel(xls或xlsx)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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