首页 > 软件开发 > 编程语言 >

如何适用c#将大量字符串导入excel

来源:互联网 2023-03-17 00:22:20 版权归原作者所有,如有侵权,请联系我们

今天的这篇经验和大家聊一聊关于如何适用c#将大量字符串导入excel的问题,希望能够帮助到有需要的朋友。ROy办公区 - 实用经验教程分享!

方法/步骤

  • 1

    /// summary> /// 导出速度最快 /// /summary> /// param name="list">列名,数据>/param> /// param name="filepath">/param> /// returns>/returns> public bool NewExport(ListDictionaryEntry> list, string filepath) { bool bSuccess = true; Microsoft.Office.Interop.Excel.Application appexcel = new Microsoft.Office.Interop.Excel.Application(); System.Reflection.Missing miss = System.Reflection.Missing.Value; appexcel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook workbookdata = null; Microsoft.Office.Interop.Excel.Worksheet worksheetdata = null; Microsoft.Office.Interop.Excel.Range rangedata; workbookdata = appexcel.Workbooks.Add();ROy办公区 - 实用经验教程分享!

    如何适用c#将大量字符串导入excelROy办公区 - 实用经验教程分享!

  • 2

    //设置对象不可见 appexcel.Visible = false; appexcel.DisplayAlerts = false; try { foreach (var lv in list) { var keys = lv.Key as Liststring>; var values = lv.Value as ListIListobject>>; worksheetdata = (Microsoft.Office.Interop.Excel.Worksheet)workbookdata.Worksheets.Add(miss, workbookdata.ActiveSheet); for (int i = 0; i keys.Count-1; i ) { //给工作表赋名称 worksheetdata.Name = keys[0];//列名的第一个数据位表名 worksheetdata.Cells[1, i 1] = keys[i 1]; } //因为第一行已经写了表头,所以所有数据都应该从a2开始 rangedata = worksheetdata.get_Range("a2", miss); Microsoft.Office.Interop.Excel.Range xlrang = null; //irowcount为实际行数,最大行 int irowcount = values.Count; int iparstedrow = 0, icurrsize = 0; //ieachsize为每次写行的数值,可以自己设置 int ieachsize = 10000; //icolumnaccount为实际列数,最大列数 int icolumnaccount = keys.Count-1; //在内存中声明一个ieachsize×icolumnaccount的数组,ieachsize是每次最大存储的行数,icolumnaccount就是存储的实际列数 object[,] objval = new object[ieachsize, icolumnaccount]; icurrsize = ieachsize; while (iparstedrow irowcount) { if ((irowcount - iparstedrow) ieachsize) icurrsize = irowcount - iparstedrow; //用for循环给数组赋值 for (int i = 0; i icurrsize; i ) ROy办公区 - 实用经验教程分享!

    如何适用c#将大量字符串导入excelROy办公区 - 实用经验教程分享!

  • 3

    { for (int j = 0; j icolumnaccount; j ) { var v = values[i iparstedrow][j]; objval[i, j] = v != null ? v.ToString() : ""; } } string X = "A" ((int)(iparstedrow 2)).ToString(); string col = ""; if (icolumnaccount = 26) { col = ((char)('A' icolumnaccount - 1)).ToString() ((int)(iparstedrow icurrsize 1)).ToString(); } else { col = ((char)('A' (icolumnaccount / 26 - 1))).ToString() ((char)('A' (icolumnaccount % 26 - 1))).ToString() ((int)(iparstedrow icurrsize 1)).ToString(); } xlrang = worksheetdata.get_Range(X, col); xlrang.NumberFormat = "@"; // 调用range的value2属性,把内存中的值赋给excel xlrang.Value2 = objval; iparstedrow = iparstedrow icurrsize; } } ((Microsoft.Office.Interop.Excel.Worksheet)workbookdata.Worksheets["Sheet1"]).Delete(); ROy办公区 - 实用经验教程分享!

    如何适用c#将大量字符串导入excelROy办公区 - 实用经验教程分享!

  • 4

    ((Microsoft.Office.Interop.Excel.Worksheet)workbookdata.Worksheets["Sheet2"]).Delete(); ((Microsoft.Office.Interop.Excel.Worksheet)workbookdata.Worksheets["Sheet3"]).Delete(); //保存工作表 workbookdata.SaveAs(filepath, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss); workbookdata.Close(false, miss, miss); appexcel.Workbooks.Close(); appexcel.Quit(); ROy办公区 - 实用经验教程分享!

    如何适用c#将大量字符串导入excelROy办公区 - 实用经验教程分享!

  • 4该信息未经授权抓取自百度经验
  • 5

    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbookdata); System.Runtime.InteropServices.Marshal.ReleaseComObject(appexcel.Workbooks); System.Runtime.InteropServices.Marshal.ReleaseComObject(appexcel); ROy办公区 - 实用经验教程分享!

    如何适用c#将大量字符串导入excelROy办公区 - 实用经验教程分享!

  • 6

    GC.Collect(); } catch (Exception ex) { ErrorMsg = ex.Message; bSuccess = false; } finally { if (appexcel != null) { ExcelImportHelper.KillSpecialExcel(appexcel); } } return bSuccess; }ROy办公区 - 实用经验教程分享!

    range.NumberFormatLocal = "@"; //设置单元格格式为文本 ROy办公区 - 实用经验教程分享!

    如何适用c#将大量字符串导入excelROy办公区 - 实用经验教程分享!

  • 注意事项

    • 以上内容和图片整理自互联网,为帮助到更多有需要的朋友所以整理在这里分享,版权归原网站所有,如果侵犯版权,请联系本网站进行删除

    以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!ROy办公区 - 实用经验教程分享!


    标签: excel编程语言

    办公区 Copyright © 2016-2023 www.bgqu.net. Some Rights Reserved. 备案号:湘ICP备2020019561号