• 常用类-excel转csv


       public class ExcelFileHelper
        {
            public static bool SaveAsCsv(string excelFilePath, string destinationCsvFilePath)
            {
    
                using (var stream = new FileStream(excelFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                {
                    IExcelDataReader reader = null;
                    if (excelFilePath.EndsWith(".xls"))
                    {
                        reader = ExcelReaderFactory.CreateBinaryReader(stream);
                    }
                    else if (excelFilePath.EndsWith(".xlsx"))
                    {
                        reader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                    }
    
                    if (reader == null)
                        return false;
    
                    var ds = reader.AsDataSet(new ExcelDataSetConfiguration()
                    {
                        ConfigureDataTable = (tableReader) => new ExcelDataTableConfiguration()
                        {
                            UseHeaderRow = false
                        }
                    });
    
                    var csvContent = string.Empty;
                    int row_no = 0;
                    while (row_no < ds.Tables[0].Rows.Count)
                    {
                        var arr = new List<string>();
                        for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                        {
                            arr.Add(ds.Tables[0].Rows[row_no][i].ToString());//需要做处理
                        }
                        row_no++;
                        csvContent += string.Join(",", arr.ToArray()) + "
    ";
                    }
                    StreamWriter csv = new StreamWriter(destinationCsvFilePath, false,Encoding.UTF8);
                    csv.Write(csvContent);
                    csv.Close();
                    return true;
                }
            }
        }

    需要安装包:

    <packages>
      <package id="ExcelDataReader" version="3.3.0" targetFramework="net451" />
      <package id="ExcelDataReader.DataSet" version="3.3.0" targetFramework="net451" />
    </packages>
  • 相关阅读:
    如何调试webservice接口是否正常
    备份数据库表
    【web】sqli-labs学习
    【web】php文件包含(利用phpinfo)
    【二进制】【WP】MOCTF逆向题解
    【web】BUUCTF-web刷题记录
    【WP】【web】中学生CTF | web部分wp
    【密码学】AES简单学习
    【密码学】CBC反转字节攻击
    【WP】攻防世界-杂项-Misc
  • 原文地址:https://www.cnblogs.com/hanliping/p/10591698.html
Copyright © 2020-2023  润新知