C# Note38: Export data into Excel
- Microsoft.Office.Interop.Excel
- You have to have Excel installed.
- Add a reference to your project to the excel interop dll. To do this on the .NET tab select Microsoft.Office.Interop.Excel. There could be multiple assemblies with this name. Select the appropriate for your Visual Studio AND Excel version.
- Here is a code sample to create a new Workbook and fill a column with the items from your list
//if you want to make excel visible
excapp.Visible = true; //create a blank workbook
var workbook = excapp.Workbooks.Add(NsExcel.XlWBATemplate.xlWBATWorksheet); //or open one - this is no pleasant, but yue're probably interested in the first parameter
string workbookPath = "C:\test.xls";
var workbook = excapp.Workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false); //Not done yet. You have to work on a specific sheet - note the cast
//You may not have any sheets at all. Then you have to add one with NsExcel.Worksheet.Add()
var sheet = (NsExcel.Worksheet)workbook.Sheets[1]; //indexing starts from 1 //do something usefull: you select now an individual cell
var range = sheet.get_Range("A1", "A1");
range.Value2 = "test"; //Value2 is not a typo //now the list
string cellName;
int counter = 1;
foreach (var item in list)
{
cellName = "A" + counter.ToString();
var range = sheet.get_Range(cellName, cellName);
range.Value2 = item.ToString();
++counter;
} //you've probably got the point by now, so a detailed explanation about workbook.SaveAs and workbook.Close is not necessary
//important: if you did not make excel visible terminating your application will terminate excel as well - I tested it
//but if you did it - to be honest - I don't know how to close the main excel window - maybee somewhere around excapp.Windows or excapp.ActiveWindow
}
Using the CSV idea
using System.IO;
using(StreamWriter sw = File.CreateText("list.csv"))
{
for(int i = 0; i < l.Count; i++)
{
sw.WriteLine(l[i]);
}
}
Using ClosedXML library( there is no need to install MS Excel
https://github.com/closedxml/closedxml/wiki
https://www.c-sharpcorner.com/UploadFile/deveshomar/exporting-generic-listt-to-excel-in-C-Sharp-using-interop/
C# Note38: Export data into Excel的更多相关文章
- NetSuite SuiteScript 2.0 export data to Excel file(xls)
In NetSuite SuiteScript, We usually do/implement export data to CSV, that's straight forward: Collec ...
- Export SQLite data to Excel in iOS programmatically(OC)
//For the app I have that did this, the SQLite data was fairly large. Therefore, I used a background ...
- csharp: Export DataSet into Excel and import all the Excel sheets to DataSet
/// <summary> /// Export DataSet into Excel /// </summary> /// <param name="send ...
- Insert data from excel to database
USE ESPA Truncate table dbo.Interface_Customer --Delete the table data but retain the structure exec ...
- Export Data from mysql Workbench 6.0
原文地址:export-data-from-mysql-workbench-6-0 问题描述 I'm trying to export my database, using MySQL Workben ...
- Tutorial: Analyzing sales data from Excel and an OData feed
With Power BI Desktop, you can connect to all sorts of different data sources, then combine and shap ...
- How to export data from Thermo-Calc 如何从Thermo-calc导出文本数据
记录20180510 问题:如何从thermo-calc导出文本数据供origin绘图? 解决: In Thermo-Calc graphical mode, you can just add a ' ...
- 1.3 Quick Start中 Step 7: Use Kafka Connect to import/export data官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Step 7: Use Kafka Connect to import/export ...
- Export GridView Data to Excel. 从GridView导出数据到Excel的奇怪问题解析
GridView导出函数内容如下 string attachment = "attachment; filename=Contacts.xls"; Respo ...
随机推荐
- Java开发笔记(二十七)数值包装类型
方法的出现缘起优化代码结构,但它的意义并不局限于此,正因为有了方法定义,编程语言才更像一门能解决实际问题的工具,而不仅仅是只能用于加减乘除的计算器.在数学的发展过程中,为了表示四则运算,人们创造了加减 ...
- .NET平台下使用MongoDB入门教程
适合人群:完全没有接触MongoDB或对MongoDB有一点了解的C#开发人员.因为本文是一篇入门级的文章. 一.了解MongoDB MongoDB是一个基于分布式文件存储的数据库.由C++语言编写 ...
- C# 消息队列-MSMQ
MQ是一种消息中间件技术,所以它能够支持多种类型的语言开发,同时也是跨平台的通信机制,也就是说MQ支持将信息转化为XML或者JSon等类型的数据存储到消息队列中,然后可以使用不同的语言来处理消息队列中 ...
- vue.js引入
开始学习vue.js,引入vue.vue.js一定要在head里面引入,实际开发中我们可能在body中引入,但是可能存在抖屏现象. 为了避免出现抖屏现象,我们引入vue.js或者jquery.js 最 ...
- prufer序列笔记
prufer序列 度娘的定义 Prufer数列是无根树的一种数列.在组合数学中,Prufer数列由有一个对于顶点标过号的树转化来的数列,点数为n的树转化来的Prufer数列长度为n-2. 对于一棵确定 ...
- 使用bfd监控静态路由,达到网络故障及时切换功能。
结论:通过BFD可以联动静态路由,从而监控整个网络上的网络情况,当出现故障时及时进行切换. 下面的例子,就是通过BFD监控上面的这个往返路由,当中间网络出现故障时,两端全部切换到下面的第二条路由进行通 ...
- 监控 redis 执行命令
监控 redis 执行命令 Intro 最近在用 redis 的时候想看看执行了哪些命令,于是发现了 redis-cli 提供的 Monitor 命令,直接使用这个就可以监控执行的大部分 redis ...
- 注册Github过程
第一步当然是建立自己的账号密码了: 一: github官网地址:https://github.com/ (1)第一步:首先起一个属于自己用户的名字(username),用户名字只能包含字母数字的字符或 ...
- huffman树即Huffma编码的实现
自己写的Huffman树生成与Huffman编码实现 (实现了核心功能 ,打出了每个字符的huffman编码 其他的懒得实现了,有兴趣的朋友可以自己在我的基础增加功能 ) /* 原创文章 转载请附上原 ...
- powerdesigner生成mysql带注释的ER图
1.安装PowerDesigner的 参考 https://blog.csdn.net/sinat_34104446/article/details/79885141 2配置逆向工程 2.1新建模型p ...