首页 > 编程知识 正文

英语音标怎么读,c与c 区别

时间:2023-05-03 09:12:37 阅读:127432 作者:2048

在cs项目中,经常需要导出文件,尤其是Excel,今天就写了一个通用方法。
引用:NPOI.dll、NPOI.OOXML.dll, 下载地址
优点:1、无需安装office 2、可以操作每个单元格 3、免费

代码如下:

namespace Test{ class Program { static void Main(string[] args) { //要显示的列(选择你要显示的列,不一定是整个对象的列) Dictionary<string, string> dic = new Dictionary<string, string>() { { "name", "姓名" }, { "age", "年龄" }, { "gender", "性别" } }; //对象集合 List<Person> list = new List<Person>(); list.Add(new Person() { name = "hhddy", age = 12, gender = "男" }); list.Add(new Person() { name = "mrdxt", age = 13, gender = "男" }); list.Add(new Person() { name = "贤惠的小松鼠", age = 5, gender = "女" }); //保存文件路径--->这个需要你自己去获取 string path = "C:/Users/MY/Desktop/临时用/person.xls"; //调用通用方法,重要得是理解如何每个sheet页、每行、每列的赋值 bool ret = NPOIHelper.PageSaveToXls(path, list, dic); Console.WriteLine(ret); Console.ReadKey(); } } /// <summary> /// NPOI Excel 帮助类 /// </summary> class NPOIHelper { /// <summary> /// 保存到excel /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path">保存路径</param> /// <param name="list">要存存储得集合</param> /// <param name="dic">字段名</param> /// <returns></returns> public static bool PageSaveToXls<T>(string path, List<T> list, Dictionary<string, string> dic) { bool isSave = false; try { if (list == null || list.Count == 0) return isSave; //excel IWorkbook book = null; //excel的sheet页 ISheet sheet = null; //excel的sheet页的每一行 IRow row = null; using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite)) { //扩展名 string extension = Path.GetExtension(path); switch (extension.ToLower()) { case ".xls": book = new HSSFWorkbook();//NPOI.dll break; case ".xlsx": book = new XSSFWorkbook();//NPOI.OOXML.dll break; } Type val = null; //所有列 List<string> fields = dic.Keys.ToList(); //所有列对应的名称 List<string> fieldsName = dic.Values.ToList(); int columnIndex = dic.Count(); int rowIndex = 0; int page = 1; foreach (T item in list) { val = item.GetType(); //写表头 if (rowIndex == 0) { sheet = book.CreateSheet("Sheet" + page); row = sheet.CreateRow(rowIndex); for (int i = 0; i < columnIndex; i++) { row.CreateCell(i, CellType.String).SetCellValue(fieldsName[i]); } rowIndex++; } //写数据 row = sheet.CreateRow(rowIndex); for (int i = 0; i < columnIndex; i++) { row.CreateCell(i, CellType.String).SetCellValue(Convert.ToString(val.GetProperty(fields[i]).GetValue(item))); } rowIndex++; //大于60000行数据就换页 if (rowIndex > 60000) { rowIndex = 0; page++; } } book.Write(stream); stream.Close(); isSave = true; } } catch (Exception ex) { //输出日志 } return isSave; } } /// <summary> /// 对象 /// </summary> class Person { public string name { get; set; } public int age { get; set; } public string gender { get; set; } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。