1,在网上搜索到的一般是这种通用的方法,这个方法速度太慢了,代码如下
private void ExportExcel(string fileName, DataGridView myDGV) { string saveFileName = ""; SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "xls"; saveDialog.Filter = "Excel文件|*.xls"; saveDialog.FileName = fileName; saveDialog.ShowDialog(); saveFileName = saveDialog.FileName; if (saveFileName.IndexOf(":") < 0) return; //被点了取消 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); if (xlApp == null) { MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel"); return; } Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks; Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet); Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1 //写入标题 for (int i = 0; i < myDGV.ColumnCount; i++) { worksheet.Cells[1, i + 1] = myDGV.Columns[i].HeaderText; } //写入数值 for (int r = 0; r < myDGV.Rows.Count; r++) { for (int i = 0; i < myDGV.ColumnCount; i++) { worksheet.Cells[r + 2, i + 1] = myDGV.Rows[r].Cells[i].Value; } System.Windows.Forms.Application.DoEvents(); } worksheet.Columns.EntireColumn.AutoFit();//列宽自适应 if (saveFileName != "") { try { workbook.Saved = true; workbook.SaveCopyAs(saveFileName); } catch (Exception ex) { MessageBox.Show("导出文件时出错,文件可能正被打开! " + ex.Message); } } xlApp.Quit(); GC.Collect();//强行销毁 MessageBox.Show("文件: " + fileName + ".xls 保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); }
2,最近研究了半天,使用NOPI的方法真是的太快了,秒存的感觉
【2.1】使用:
private void btn_export_Click(object sender, EventArgs e) { try { ExcelHelper.ExportToExcel(this.dgv_data); new FrmConfirmSingle("日志导出", "日志记录导出成功") { TopMost = true }.ShowDialog(); } catch (Exception exception) { new FrmConfirmSingle("日志导出", "日志记录导出失败:"+exception.Message) { TopMost = true }.ShowDialog(); } }
【2.2】主要代码如下:
/// <summary> /// 由DataGridView导出Excel /// </summary> /// <param name="grid"></param> /// <param name="sheetName"></param> /// <param name="filePath"></param> /// <returns></returns> public static string ExportToExcel(DataGridView grid, string sheetName = "result", string filePath = null) { if (grid.Rows.Count <= 0) return null; if (string.IsNullOrEmpty(filePath)) { filePath = GetSaveFilePath(); } if (string.IsNullOrEmpty(filePath)) return null; bool isCompatible = GetIsCompatible(filePath); IWorkbook workbook = CreateWorkbook(isCompatible); ICellStyle cellStyle = GetCellStyle(workbook); ISheet sheet = workbook.CreateSheet(sheetName); IRow headerRow = sheet.CreateRow(0); for (int i = 0; i < grid.Columns.Count; i++) { ICell cell = headerRow.CreateCell(i); cell.SetCellValue(grid.Columns[i].Name); cell.CellStyle = cellStyle; } int rowIndex = 1; foreach (DataGridViewRow row in grid.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); for (int n = 0; n < grid.Columns.Count; n++) { dataRow.CreateCell(n).SetCellValue((row.Cells[n].Value ?? "").ToString()); } rowIndex++; } AutoColumnWidth(sheet, headerRow.LastCellNum - 1); FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); workbook.Write(fs); fs.Dispose(); sheet = null; headerRow = null; workbook = null; MessageBox.Show("文件: " + filePath + ".xls 保存成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return filePath; }
【2.3】下面是我整理的通用EXCEL的帮助类,非常的实用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Data; using System.Windows.Forms; using NPOI.SS.UserModel; using NPOI.HSSF.UserModel; using NPOI.XSSF.UserModel; using System.Collections; using System.Runtime.CompilerServices; /* * 须在项目中添加引用 NPOI.dll NPOI.OOXML.dll NPOI.OPenXml4Net.dll */ namespace AutomaticStoreMotionDal { public static class ExcelHelper { /// <summary> /// 获取要保存的文件名称(含完整路径) /// </summary> /// <returns></returns> public static string GetSaveFilePath() { SaveFileDialog saveFileDig = new SaveFileDialog(); saveFileDig.Filter = "Excel Office97-2003(*.xls)|*.xls|Excel Office2007及以上(*.xlsx)|*.xlsx"; saveFileDig.FileName = DateTime.Now.ToString("yyyyMMddHHmmss"); saveFileDig.FilterIndex = 0; saveFileDig.OverwritePrompt = true; string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//获取当前系统桌面路径 saveFileDig.InitialDirectory = dir; string filePath = null; if (saveFileDig.ShowDialog() == DialogResult.OK) { filePath = saveFileDig.FileName; } return filePath; } /// <summary> /// 打开文件对话框,并返回文件的路径 /// </summary> /// <returns></returns> public static string GetOpenFilePath() { //创建对话框的对象 OpenFileDialog ofd = new OpenFileDialog(); //设置对话框的标题 ofd.Title = "请选择要打开的文件"; //设置对话框可以多选 ofd.Multiselect = true; //设置对话框的初始目录 ofd.InitialDirectory = @"C:UsersAdministratorDesktop"; //设置对话框打开文件的类型 ofd.Filter = "Excel文件(.xls)|*.xls|Excel文件(.xlsx)|*.xlsx"; //展示对话框 ofd.ShowDialog(); //获得在打开对话框中选中的文件的路径 string filePath = ofd.FileName;//全路径 return filePath; } /// <summary> /// 判断Excel文件是否为兼容模式(.xls) /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static bool GetIsCompatible(string filePath) { return filePath.EndsWith(".xls", StringComparison.OrdinalIgnoreCase); } /// <summary> /// 创建工作薄 /// </summary> /// <param name="isCompatible">true就是.xls</param> /// <returns></returns> public static IWorkbook CreateWorkbook(bool isCompatible) { if (isCompatible) { return new HSSFWorkbook(); } else { return new XSSFWorkbook(); } } /// <summary> /// 创建工作薄(依据文件流) /// </summary> /// <param name="isCompatible"></param> /// <param name="stream"></param> /// <returns></returns> public static IWorkbook CreateWorkbook(bool isCompatible, Stream stream) { if (isCompatible) { return new HSSFWorkbook(stream); } else { return new XSSFWorkbook(stream); } } #region 传入一个文件路径,返回一个IWorkbook对象 /// <summary> /// 传入一个文件路径,返回一个IWorkbook对象 /// </summary> /// <param name="filepath"></param> /// <returns></returns> public static IWorkbook CreateWorkbook(string filepath) { IWorkbook workbook = null; bool isCompatible = ExcelHelper.GetIsCompatible(filepath); using (FileStream fs = File.Open(filepath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //把xls文件读入workbook变量里,之后就可以关闭了 workbook = ExcelHelper.CreateWorkbook(isCompatible, fs); fs.Close(); } return workbook; } #endregion #region 打开一个excel文件,设置单元格的值,再保存文件 /// <summary> /// 打开一个excel文件,设置单元格的值,再保存文件 /// </summary> /// <param name="ExcelPath"></param> /// <param name="sheetname"></param> /// <param name="column"></param> /// <param name="row"></param> /// <param name="value"></param> /// <returns></returns> public static bool SetCellValue(String ExcelPath, String sheetname, int column, int row, String value) { bool returnb = false; try { IWorkbook wk = null; bool isCompatible = ExcelHelper.GetIsCompatible(ExcelPath); using (FileStream fs = File.Open(ExcelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //把xls文件读入workbook变量里,之后就可以关闭了 wk = ExcelHelper.CreateWorkbook(isCompatible, fs); fs.Close(); } //把xls文件读入workbook变量里,之后就可以关闭了 //ISheet sheet = wk.GetSheet(sheetname); ISheet sheet = wk.GetSheetAt(0); ICell cell = sheet.GetRow(row).GetCell(column); cell.SetCellValue(value); using (FileStream fileStream = File.Open(ExcelPath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { wk.Write(fileStream); fileStream.Close(); } returnb = true; } catch (Exception) { returnb = false; throw; } return returnb; } #endregion #region 打开一个文件,读取excel文件某个单元格的值(多少行,多少列) /// <summary> /// 打开一个文件,读取excel文件某个单元格的值(多少行,多少列) /// </summary> /// <param name="ExcelPath"></param> /// <param name="sheetname"></param> /// <param name="column"></param> /// <param name="row"></param> /// <returns></returns> public static String GetCellValue(string ExcelPath, String sheetname, int column, int row) { String returnStr = null; try { IWorkbook wk = null; bool isCompatible = ExcelHelper.GetIsCompatible(ExcelPath); using (FileStream fs = File.Open(ExcelPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //把xls文件读入workbook变量里,之后就可以关闭了 wk = ExcelHelper.CreateWorkbook(isCompatible, fs); fs.Close(); } //把xls文件读入workbook变量里,之后就可以关闭了 //ISheet sheet = wk.GetSheet(sheetname); ISheet sheet = wk.GetSheetAt(0); ICell cell = sheet.GetRow(row).GetCell(column); returnStr = cell.ToString(); } catch (Exception) { returnStr = "Exception"; throw; } return returnStr; } #endregion #region 打开一个文件,删除多少行以后的数据(是删除,不是清空数据) /// <summary> /// 打开一个文件,删除多少行以后的数据(是删除,不是清空数据) /// </summary> /// <param name="fileMatchPath"></param> /// <param name="rowIndex">从多少行后开始删除</param> public static void DelRowsData(string fileMatchPath, int startRowIndex) { IWorkbook wk = null; bool isCompatible = ExcelHelper.GetIsCompatible(fileMatchPath); using (FileStream fs = File.Open(fileMatchPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //把xls文件读入workbook变量里,之后就可以关闭了 wk = ExcelHelper.CreateWorkbook(isCompatible, fs); fs.Close(); } ISheet sheet = wk.GetSheetAt(0); for (int i = startRowIndex; i <= sheet.LastRowNum; i++) { if (sheet.GetRow(i) == null) { i++; continue; } sheet.RemoveRow(sheet.GetRow(i)); } //转为字节数组 MemoryStream stream = new MemoryStream(); wk.Write(stream); var buf = stream.ToArray(); //保存为Excel文件 这种方式能保存.xls和.xlsx文件 using (FileStream fs = new FileStream(fileMatchPath, FileMode.Create, FileAccess.Write)) { fs.Write(buf, 0, buf.Length); fs.Flush(); } } #endregion /// <summary> /// 创建表格头单元格 /// </summary> /// <param name="sheet"></param> /// <returns></returns> private static ICellStyle GetCellStyle(IWorkbook workbook) { ICellStyle style = workbook.CreateCellStyle(); style.FillPattern = FillPattern.SolidForeground; style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index; return style; } /// <summary> /// 遍历打印二维数组 /// </summary> /// <param name="array"></param> public static void PrintTwoArrayTest(object[,] array) { Console.WriteLine("============测试打印二维数组=============="); int row = array.GetLength(0); int column = array.GetLength(1); for (int r = 0; r < row; r++) { for (int c = 0; c < column; c++) { if (array[r, c] != null) { string value = array[r, c].ToString(); Console.Write($"{value} |"); } } Console.WriteLine(); } } /// <summary> /// 传入2个二维数组,进行条件匹配替换,返回替换后的一个二维数组 /// </summary> /// <param name="refArray">参考的数组</param> /// <param name="matchArray">带替换的数组</param> /// <param name="refColumn01">参考列1</param> /// <param name="refColumn02">参考列2</param> /// <param name="refColTarget01">被复制的值的列1</param> /// <param name="matchColumn01">带替换的参考列1</param> /// <param name="matchColumn02">带替换的参考列2</param> /// <param name="matchColTarget01">带粘贴的值的列1</param> /// <returns></returns> public static string[,] GetMatchArray(string[,] refArray, string[,] matchArray, int refColumn01, int refColumn02, int refColTarget01, int matchColumn01, int matchColumn02, int matchColTarget01) { Console.WriteLine("============遍历2个二维数,匹配替换=============="); int row = refArray.GetLength(0); int column = matchArray.GetLength(1); int row02 = matchArray.GetLength(0); int iMatch = 0; for (int r = 0; r < row; r++) { string value01 = refArray[r, refColumn01];//第1列的数据 string value02 = refArray[r, refColumn02];//第2列的数据 if (value01 != null && value02 != null) { if (value01.Length > 0 | value02.Length > 0) { for (int r02 = 0; r02 < row02; r02++) { string match01 = matchArray[r02, matchColumn01];//第1列的数据 string match02 = matchArray[r02, matchColumn02];//第2列的数据 if (value01 == match01 && value02 == match02) { matchArray[r02, matchColTarget01] = refArray[r, refColTarget01]; iMatch++; Console.WriteLine($"匹配了{iMatch}次"); } } } } } return matchArray; } /// <summary> /// 传入2个数组,根据相同条件匹配,吧ref的目标写入match中 /// </summary> /// <param name="refArray">参考的数组</param> /// <param name="matchArray">带替换的数组</param> /// <param name="refColumn01"></param> /// <param name="refColTarget01"></param> /// <param name="matchColumn01"></param> /// <param name="matchColTarget01"></param> /// <returns></returns> public static string[,] GetMatchArray(string[,] refArray, string[,] matchArray, int refColumn01, int refColTarget01, int matchColumn01, int matchColTarget01) { Console.WriteLine("============遍历2个二维数,匹配替换=============="); int row = refArray.GetLength(0); int column = matchArray.GetLength(1); int row02 = matchArray.GetLength(0); int iMatch = 0; for (int r = 0; r < row; r++) { string value01 = string.Empty; value01 = refArray[r, refColumn01];//遍历第一个数组第1列的数据 //value01 = value01.Trim(); if (value01 != null) { if (value01.Length > 0) { for (int r02 = 0; r02 < row02; r02++) { string match01 = string.Empty; match01 = matchArray[r02, matchColumn01];//遍历第一个数组第1列的数据 //match01 = match01.Trim(); if (value01 == match01) { matchArray[r02, matchColTarget01] = refArray[r, refColTarget01]; iMatch++; Console.WriteLine($"匹配了{iMatch}次"); } } } } } return matchArray; } /// <summary> /// 遍历一个数组,如果第二列的数值大于等于第一列的数值,替换字符串 /// </summary> /// <param name="matchArray"></param> /// <param name="refColumn01"></param> /// <param name="refColumn02"></param> /// <param name="sValue"></param> /// <returns></returns> public static string[,] GetMatchArray(string[,] matchArray, int refColumn01, int refColumn02, string sValue) { Console.WriteLine("============遍历2个二维数,匹配替换=============="); int row = matchArray.GetLength(0); int column = matchArray.GetLength(1); int iMatch = 0; for (int r = 0; r < row; r++) { string value01 = matchArray[r, refColumn01];//第1列的数据 string value02 = matchArray[r, refColumn02];//第2列的数据 try { int i01 = Convert.ToInt32(value01); int i02 = Convert.ToInt32(value02); if (i01 >= i02) { matchArray[r, refColumn02] = sValue + $"(数量:{value02})"; } } catch { } } return matchArray; } #region 打开excel文件,获取某一行的数据 /// <summary> /// 打开excel文件,获取某一行的数据 /// </summary> /// <param name="filepath">文件全路径</param> /// <param name="iRow">哪一行的数据</param> /// <param name="sheet_Number">哪一个sheet表</param> /// <returns></returns> public static ArrayList GetRowData(string filepath, int sheet_Number, int iRow) { ArrayList arrayList = new ArrayList(); bool isCompatible = ExcelHelper.GetIsCompatible(filepath); using (FileStream fsRead = File.OpenRead(filepath)) { IWorkbook workbook = ExcelHelper.CreateWorkbook(isCompatible, fsRead); ISheet sheet = workbook.GetSheetAt(sheet_Number - 1); IRow currentRow = sheet.GetRow(iRow - 1); for (int c = 0; c < currentRow.LastCellNum; c++) { //获取每个单元格(r行c列的数据) ICell cell = currentRow.GetCell(c); //获取单元格的内容 string value = string.Empty; if (cell != null) { value = cell.ToString(); //如果单元格为空,这里会报错的 arrayList.Add(value); } } return arrayList; } } #endregion /// <summary> /// 打开excel文件,根据某一行的数据,根据字符串内容,返回这个字符串所在的列的索引 /// </summary> /// <param name="filepath"></param> /// <param name="iRow"></param> /// <param name="sheet_Number">从1开始的</param> /// <param name="s1">注意字符串的顺序</param> /// <param name="s2"></param> /// <param name="s3"></param> /// <returns></returnss public static ArrayList GetDataIndexs(string filepath, int sheet_Number, int iRow, string s1, string s2, string s3) { ArrayList arrayList = new ArrayList(); bool isCompatible = ExcelHelper.GetIsCompatible(filepath); using (FileStream fsRead = File.OpenRead(filepath)) { IWorkbook workbook = ExcelHelper.CreateWorkbook(isCompatible, fsRead); ISheet sheet = workbook.GetSheetAt(sheet_Number - 1); IRow currentRow = sheet.GetRow(iRow - 1); for (int c = 0; c < currentRow.LastCellNum; c++) { //获取每个单元格(r行c列的数据) ICell cell = currentRow.GetCell(c); //获取单元格的内容 string value = string.Empty; if (cell != null) { value = cell.ToString(); //如果单元格为空,这里会报错的 if (value == s1 | value == s2 || value == s3) { arrayList.Add(c); } } } Console.WriteLine("==========测试打印索引值============"); foreach (var a in arrayList) { Console.WriteLine($"{a} |"); } return arrayList; } } /// <summary> /// 打开excel文件,根据某一行的字符串,然后这个字符串所在列的索引 /// </summary> /// <param name="filepath"></param> /// <param name="sheet_Number"></param> /// <param name="iRow"></param> /// <param name="sValue"></param> /// <returns></returns> public static int GetDataIndex(string filepath, int sheet_Number, int iRow, string sValue) { int i = 0; bool isCompatible = ExcelHelper.GetIsCompatible(filepath); using (FileStream fsRead = File.OpenRead(filepath)) { IWorkbook workbook = ExcelHelper.CreateWorkbook(isCompatible, fsRead); ISheet sheet = workbook.GetSheetAt(sheet_Number - 1); IRow currentRow = sheet.GetRow(iRow - 1); for (int c = 0; c < currentRow.LastCellNum; c++) { //获取每个单元格(r行c列的数据) ICell cell = currentRow.GetCell(c); //获取单元格的内容 string value = string.Empty; if (cell != null) { value = cell.ToString(); //如果单元格为空,这里会报错的 if (value == sValue) { i = c; } } } } return i; } /// <summary> /// 打开一个文件,把第几行的数据取出来,返回一个字典 单元格的值:列的索引 /// </summary> /// <param name="filepath"></param> /// <param name="sheet_Number">第几张工作表(从1开始)</param> /// <param name="iRow">第几行(从1开始)</param> /// <returns></returns> public static Dictionary<string, int> GetDataDictionary(string filepath, int sheet_Number, int iRow) { Dictionary<string, int> DataDict = new Dictionary<string, int>(); bool isCompatible = ExcelHelper.GetIsCompatible(filepath); using (FileStream fsRead = File.OpenRead(filepath)) { IWorkbook workbook = ExcelHelper.CreateWorkbook(isCompatible, fsRead); ISheet sheet = workbook.GetSheetAt(sheet_Number - 1); IRow currentRow = sheet.GetRow(iRow - 1); for (int c = 0; c < currentRow.LastCellNum; c++) { //获取每个单元格(r行c列的数据) ICell cell = currentRow.GetCell(c); //获取单元格的内容 string value = string.Empty; if (cell != null) { value = cell.ToString(); //如果单元格为空,这里会报错的 if (!DataDict.ContainsKey(value)) { if (value == "*预计交货日期" | value == "预计交货日期") { value = "*预计交货日期"; } DataDict.Add(value, c); } else { if (filepath.Contains("销售订单")) //销售订单模板的第二个备注填写收货地址 { if (value == "备注") //如果有两个备注 { //DataDict.Add("采购员", c); DataDict.Add("收货地址", c); } } } } } //Console.WriteLine("================开始遍历字典==============="); //foreach (KeyValuePair<string, int> kv in DataDict)//通过KeyValuePair遍历元素 //{ // Console.WriteLine($"Key:{kv.Key},Value:{kv.Value}"); //} return DataDict; } } /// <summary> /// 打开一个文件,根据第几张表第几行的中的两个字符,返回值:一个字典 /// </summary> /// <param name="filepath"></param> /// <param name="sheet_Number"></param> /// <param name="strColumnKey"></param> /// <param name="strColumnValue"></param> /// <returns></returns> public static Dictionary<string, string> GetDataDictionary(string filepath, int sheet_Number, int iRow, string strColumnKey, string strColumnValue) { Dictionary<string, int> dic = GetDataDictionary(filepath, 1, iRow); int iColumnKey = dic[strColumnKey]; int iColumnValue = dic[strColumnValue]; Dictionary<string, string> DataDict = new Dictionary<string, string>(); bool isCompatible = ExcelHelper.GetIsCompatible(filepath); using (FileStream fsRead = File.OpenRead(filepath)) { IWorkbook workbook = ExcelHelper.CreateWorkbook(isCompatible, fsRead); ISheet sheet = workbook.GetSheetAt(sheet_Number - 1); for (int i = 0; i <= sheet.LastRowNum; i++) { IRow rowdata = sheet.GetRow(i); if (rowdata != null) { ICell cellKey = rowdata.GetCell(iColumnKey);//如果rowdata是null,这里报错 ICell cellValue = rowdata.GetCell(iColumnValue); if (cellKey != null && cellValue != null) { if (!DataDict.ContainsKey(cellKey.ToString())) { string strCellKey = cellKey.ToString(); string strCellValue = cellValue.ToString(); DataDict.Add(strCellKey, strCellValue); } } } } return DataDict; } } /// <summary> /// 自适应列宽 /// </summary> /// <param name="sheet"></param> /// <param name="cols"></param> public static void AutoColumnWidth(ISheet sheet, int cols) { for (int col = 0; col <= cols; col++) { sheet.AutoSizeColumn(col);//自适应宽度,但是其实还是比实际文本要宽 int columnWidth = sheet.GetColumnWidth(col) / 256;//获取当前列宽度 for (int rowIndex = 1; rowIndex <= sheet.LastRowNum; rowIndex++) { IRow row = sheet.GetRow(rowIndex); ICell cell = row.GetCell(col); if (cell != null) { int contextLength = Encoding.UTF8.GetBytes(cell.ToString()).Length;//获取当前单元格的内容宽度 columnWidth = columnWidth < contextLength ? contextLength : columnWidth; } } sheet.SetColumnWidth(col, columnWidth * 200);//经过测试200比较合适。 } } /// <summary> /// 自适应列宽和打印页缩放 /// </summary> /// <param name="filePath">excel文件路径</param> /// <param name="scale">缩放比例(77,80,100等)</param> public static void AutoColumnWidth(string filePath,short scale=80) { //【1】打开excel文件的第几张表,第几行的数据,返回一个字典{列名:列的索引} //字典的作用:可以根据列名快速找到对应的列索引 Dictionary<string, int> dicData = ExcelHelper.GetDataDictionary(filePath, 1, 1); bool isCompatible = ExcelHelper.GetIsCompatible(filePath); IWorkbook workbook = null; using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //把xls文件读入workbook变量里,之后就可以关闭了 workbook = ExcelHelper.CreateWorkbook(isCompatible, fs); fs.Close(); } ISheet sheet = workbook.GetSheetAt(0); int rowCount = sheet.LastRowNum; for (int col = 0; col < sheet.GetRow(0).LastCellNum; col++) { //自适应列宽 sheet.AutoSizeColumn(col); } //sheet.PrintSetup.FitWidth = 1; //sheet.PrintSetup.FitHeight = 0; //设置打印页面缩放比例 sheet.PrintSetup.Scale = scale; #region 非常的耗时,不推荐使用 ////开始遍历【遍历行操作】 //for (int r = 1; r <= rowCount; r++) //从第二行开始遍历 //{ // IRow currentRow = sheet.GetRow(r); //读取当前行数据 // if (currentRow == null) //如果为空,重新创建一行,防止null报错 // { // sheet.CreateRow(r); // currentRow = sheet.GetRow(r); // } // for (int i = 0; i < currentRow.LastCellNum; i++) // { // //sheet.AutoSizeColumn(i);//非常的耗时,不推荐用 // } //} #endregion //转为字节数组 MemoryStream stream = new MemoryStream(); workbook.Write(stream); var buf = stream.ToArray(); //保存为Excel文件 这种方式能保存.xls和.xlsx文件 using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { fs.Write(buf, 0, buf.Length); fs.Flush(); } } #region 根据一个文件,获取一个Excel文件的最大行数和列数(不成熟不建议用) /// <summary> /// 根据一个文件,获取一个Excel文件的最大行数和列数(不成熟不建议用) /// </summary> /// <param name="filepath">excel表格保存的地址,包括"文件名.xls</param> /// <param name="sheet_number">代表将要读取的sheet表的索引位置</param> /// <returns>行数Array[0],列数Array[2]</returns> public static Array GetRowCountAndColumnCount(string filepath, int sheet_number) { int rowMaxCount = 0; int columnMaxCount = 0; FileStream readStream = null; try { if (!string.IsNullOrEmpty(filepath) && sheet_number > 0) { readStream = new FileStream(filepath, FileMode.Open, FileAccess.Read); bool isCompatible = GetIsCompatible(filepath); IWorkbook workbook = CreateWorkbook(isCompatible, readStream); ISheet sheet = workbook.GetSheetAt(sheet_number - 1); if (sheet != null) { rowMaxCount = sheet.LastRowNum + 1; //有效行数(NPOI读取的有效行数不包括列头,所以需要加1) for (int c = 0; c <= sheet.LastRowNum; c++) { IRow row = sheet.GetRow(c); #pragma warning disable CS1525 // 表达式项“=”无效 if (row != null && row.LastCellNum > -1) #pragma warning restore CS1525 // 表达式项“=”无效 { columnMaxCount = row.LastCellNum; } } } } } catch (Exception e) { Console.WriteLine(e); throw; } finally { if (readStream != null) { readStream.Close(); } } int[] array = new int[2]; array[1] = rowMaxCount; array[2] = columnMaxCount; return array; } #endregion /// <summary> /// Excel转化为二维数组 /// </summary> /// <param name="filepath">文件全路径</param> /// <param name="arrayRowNnmber">数组的行数</param> /// <param name="arrayColumnNumber">数组的列数</param> /// <param name="sheet_Number">要遍历的sheet索引</param> /// <returns>返回一个二维素组</returns> public static string[,] ToTwoArray(string filepath, int arrayRowNnmber, int arrayColumnNumber, int sheet_Number) { string[,] array = new string[arrayRowNnmber, arrayColumnNumber]; bool isCompatible = ExcelHelper.GetIsCompatible(filepath); using (FileStream fsRead = File.OpenRead(filepath)) { IWorkbook workbook = ExcelHelper.CreateWorkbook(isCompatible, fsRead); ISheet sheet = workbook.GetSheetAt(sheet_Number - 1); //获取第一个工作表(sheet) int rowCount = sheet.LastRowNum; if (!isCompatible)//如果是xlsx格式的,行数要-2 { rowCount = rowCount - 2; } for (int r = 0; r <= rowCount; r++) { IRow currentRow = sheet.GetRow(r); //读取当前行数据 Console.Write($"第{r}行有{currentRow.LastCellNum}列有数据:--->"); for (int c = 0; c < currentRow.LastCellNum; c++) { //获取每个单元格(r行c列的数据) ICell cell = currentRow.GetCell(c); //获取单元格的内容 string value = string.Empty; if (cell != null) { value = cell.ToString(); //如果单元格为空,这里会报错的 } Console.Write($"{value} |"); array[r, c] = value; } Console.WriteLine(); } return array; } } /// <summary> /// 从工作表中生成DataTable /// </summary> /// <param name="sheet"></param> /// <param name="headerRowIndex"></param> /// <returns></returns> private static DataTable GetDataTableFromSheet(ISheet sheet, int headerRowIndex) { DataTable table = new DataTable(); IRow headerRow = sheet.GetRow(headerRowIndex); int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++) { if (headerRow.GetCell(i) == null || headerRow.GetCell(i).StringCellValue.Trim() == "") { // 如果遇到第一个空列,则不再继续向后读取 cellCount = i + 1; break; } DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue); table.Columns.Add(column); } for (int i = (headerRowIndex + 1); i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); if (row != null && !string.IsNullOrEmpty(row.Cells[0].StringCellValue)) { DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) { dataRow[j] = row.GetCell(j).ToString(); } } table.Rows.Add(dataRow); } } return table; } #region 公共导出方法 /// <summary> /// 由DataSet导出Excel /// </summary> /// <param name="sourceTable">要导出数据的DataTable</param> /// <returns>Excel工作表</returns> public static string ExportToExcel(DataSet sourceDs, string filePath = null) { if (string.IsNullOrEmpty(filePath)) { filePath = GetSaveFilePath(); } if (string.IsNullOrEmpty(filePath)) return null; bool isCompatible = GetIsCompatible(filePath); IWorkbook workbook = CreateWorkbook(isCompatible); ICellStyle cellStyle = GetCellStyle(workbook); for (int i = 0; i < sourceDs.Tables.Count; i++) { DataTable table = sourceDs.Tables[i]; string sheetName = "result" + i.ToString(); ISheet sheet = workbook.CreateSheet(sheetName); IRow headerRow = sheet.CreateRow(0); // handling header. foreach (DataColumn column in table.Columns) { ICell cell = headerRow.CreateCell(column.Ordinal); cell.SetCellValue(column.ColumnName); cell.CellStyle = cellStyle; } // handling value. int rowIndex = 1; foreach (DataRow row in table.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in table.Columns) { dataRow.CreateCell(column.Ordinal).SetCellValue((row[column] ?? "").ToString()); } rowIndex++; } } FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); workbook.Write(fs); fs.Dispose(); workbook = null; return filePath; } /// <summary> /// 获取sheet表名 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public static string[] GetSheetName(string filePath) { int sheetNumber = 0; var file = new FileStream(filePath, FileMode.Open, FileAccess.Read); if (filePath.IndexOf(".xlsx") > 0) { //2007版本 var xssfworkbook = new XSSFWorkbook(file); sheetNumber = xssfworkbook.NumberOfSheets; string[] sheetNames = new string[sheetNumber]; for (int i = 0; i < sheetNumber; i++) { sheetNames[i] = xssfworkbook.GetSheetName(i); } return sheetNames; } else if (filePath.IndexOf(".xls") > 0) { //2003版本 var hssfworkbook = new HSSFWorkbook(file); sheetNumber = hssfworkbook.NumberOfSheets; string[] sheetNames = new string[sheetNumber]; for (int i = 0; i < sheetNumber; i++) { sheetNames[i] = hssfworkbook.GetSheetName(i); } return sheetNames; } return null; } /// <summary> /// 根据表名获取表 /// </summary> /// <param name="filePath"></param> /// <param name="sheetName"></param> /// <returns></returns> public static DataTable ExcelToDataTable(string filePath, string sheetName) { string outMsg = ""; var dt = new DataTable(); string fileType = Path.GetExtension(filePath).ToLower(); try { ISheet sheet = null; FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); if (fileType == ".xlsx") { //2007版 XSSFWorkbook workbook = new XSSFWorkbook(fs); sheet = workbook.GetSheet(sheetName); if (sheet != null) { dt = GetSheetDataTable(sheet, out outMsg); } } else if (fileType == ".xls") { //2003版 HSSFWorkbook workbook = new HSSFWorkbook(fs); sheet = workbook.GetSheet(sheetName); if (sheet != null) { dt = GetSheetDataTable(sheet, out outMsg); } } } catch (Exception e) { Console.WriteLine(e.Message); } return dt; } private static int sheetCellNumMax = 12; /// <summary> /// 获取sheet表对应的DataTable /// </summary> /// <param name="sheet">Excel工作表</param> /// <param name="strMsg"></param> /// <returns></returns> private static DataTable GetSheetDataTable(ISheet sheet, out string strMsg) { strMsg = ""; DataTable dt = new DataTable(); string sheetName = sheet.SheetName; int startIndex = 0;// sheet.FirstRowNum; int lastIndex = sheet.LastRowNum; //最大列数 int cellCount = 0; IRow maxRow = sheet.GetRow(0); for (int i = startIndex; i <= lastIndex; i++) { IRow row = sheet.GetRow(i); if (row != null && cellCount < row.LastCellNum) { cellCount = row.LastCellNum; maxRow = row; } } //列名设置 try { //maxRow.LastCellNum = 12 // L for (int i = 0; i < sheetCellNumMax; i++)//maxRow.FirstCellNum { dt.Columns.Add(Convert.ToChar(((int)'A') + i).ToString()); //DataColumn column = new DataColumn("Column" + (i + 1).ToString()); //dt.Columns.Add(column); } } catch { strMsg = "工作表" + sheetName + "中无数据"; return null; } //数据填充 for (int i = startIndex; i <= lastIndex; i++) { IRow row = sheet.GetRow(i); DataRow drNew = dt.NewRow(); if (row != null) { for (int j = row.FirstCellNum; j < row.LastCellNum; ++j) { if (row.GetCell(j) != null) { ICell cell = row.GetCell(j); switch (cell.CellType) { case CellType.Blank: drNew[j] = ""; break; case CellType.Numeric: short format = cell.CellStyle.DataFormat; //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理 if (format == 14 || format == 31 || format == 57 || format == 58) drNew[j] = cell.DateCellValue; else drNew[j] = cell.NumericCellValue; if (cell.CellStyle.DataFormat == 177 || cell.CellStyle.DataFormat == 178 || cell.CellStyle.DataFormat == 188) drNew[j] = cell.NumericCellValue.ToString("#0.00"); break; case CellType.String: drNew[j] = cell.StringCellValue; break; case CellType.Formula: try { drNew[j] = cell.NumericCellValue; if (cell.CellStyle.DataFormat == 177 || cell.CellStyle.DataFormat == 178 || cell.CellStyle.DataFormat == 188) drNew[j] = cell.NumericCellValue.ToString("#0.00"); } catch { try { drNew[j] = cell.StringCellValue; } catch { } } break; default: drNew[j] = cell.StringCellValue; break; } } } } dt.Rows.Add(drNew); } return dt; } /// <summary> /// 由DataTable导出Excel /// </summary> /// <param name="sourceTable">要导出数据的DataTable</param> /// <returns>Excel工作表</returns> public static string ExportToExcel(DataTable sourceTable, string sheetName = "result", string filePath = null) { if (sourceTable.Rows.Count <= 0) return null; if (string.IsNullOrEmpty(filePath)) { filePath = GetSaveFilePath(); } if (string.IsNullOrEmpty(filePath)) return null; bool isCompatible = GetIsCompatible(filePath); IWorkbook workbook = CreateWorkbook(isCompatible); ICellStyle cellStyle = GetCellStyle(workbook); ISheet sheet = workbook.CreateSheet(sheetName); IRow headerRow = sheet.CreateRow(0); // handling header. foreach (DataColumn column in sourceTable.Columns) { ICell headerCell = headerRow.CreateCell(column.Ordinal); headerCell.SetCellValue(column.ColumnName); headerCell.CellStyle = cellStyle; } // handling value. int rowIndex = 1; foreach (DataRow row in sourceTable.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in sourceTable.Columns) { dataRow.CreateCell(column.Ordinal).SetCellValue((row[column] ?? "").ToString()); } rowIndex++; } FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); workbook.Write(fs); fs.Dispose(); sheet = null; headerRow = null; workbook = null; return filePath; } /// <summary> /// 由List导出Excel /// </summary> /// <typeparam name="T">类型</typeparam> /// <param name="data">在导出的List</param> /// <param name="sheetName">sheet名称</param> /// <returns></returns> public static string ExportToExcel<T>(List<T> data, IList<KeyValuePair<string, string>> headerNameList, string sheetName = "result", string filePath = null) where T : class { if (data.Count <= 0) return null; if (string.IsNullOrEmpty(filePath)) { filePath = GetSaveFilePath(); } if (string.IsNullOrEmpty(filePath)) return null; bool isCompatible = GetIsCompatible(filePath); IWorkbook workbook = CreateWorkbook(isCompatible); ICellStyle cellStyle = GetCellStyle(workbook); ISheet sheet = workbook.CreateSheet(sheetName); IRow headerRow = sheet.CreateRow(0); for (int i = 0; i < headerNameList.Count; i++) { ICell cell = headerRow.CreateCell(i); cell.SetCellValue(headerNameList[i].Value); cell.CellStyle = cellStyle; } Type t = typeof(T); int rowIndex = 1; foreach (T item in data) { IRow dataRow = sheet.CreateRow(rowIndex); for (int n = 0; n < headerNameList.Count; n++) { object pValue = t.GetProperty(headerNameList[n].Key).GetValue(item, null); dataRow.CreateCell(n).SetCellValue((pValue ?? "").ToString()); } rowIndex++; } FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); workbook.Write(fs); fs.Dispose(); sheet = null; headerRow = null; workbook = null; return filePath; } /// <summary> /// 由DataGridView导出 /// </summary> /// <param name="grid"></param> /// <param name="sheetName"></param> /// <param name="filePath"></param> /// <returns></returns> public static string ExportToExcel(DataGridView grid, string sheetName = "result", string filePath = null) { if (grid.Rows.Count <= 0) return null; if (string.IsNullOrEmpty(filePath)) { filePath = GetSaveFilePath(); } if (string.IsNullOrEmpty(filePath)) return null; bool isCompatible = GetIsCompatible(filePath); IWorkbook workbook = CreateWorkbook(isCompatible); ICellStyle cellStyle = GetCellStyle(workbook); ISheet sheet = workbook.CreateSheet(sheetName); IRow headerRow = sheet.CreateRow(0); for (int i = 0; i < grid.Columns.Count; i++) { ICell cell = headerRow.CreateCell(i); cell.SetCellValue(grid.Columns[i].Name); cell.CellStyle = cellStyle; } int rowIndex = 1; foreach (DataGridViewRow row in grid.Rows) { IRow dataRow = sheet.CreateRow(rowIndex); for (int n = 0; n < grid.Columns.Count; n++) { dataRow.CreateCell(n).SetCellValue((row.Cells[n].Value ?? "").ToString()); } rowIndex++; } FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); workbook.Write(fs); fs.Dispose(); sheet = null; headerRow = null; workbook = null; return filePath; } #endregion #region 公共导入方法 /// <summary> /// 由Excel导入DataTable /// </summary> /// <param name="excelFileStream">Excel文件流</param> /// <param name="sheetName">Excel工作表名称</param> /// <param name="headerRowIndex">Excel表头行索引</param> /// <param name="isCompatible">是否为兼容模式</param> /// <returns>DataTable</returns> public static DataTable ImportFromExcel(Stream excelFileStream, string sheetName, int headerRowIndex, bool isCompatible) { IWorkbook workbook = CreateWorkbook(isCompatible, excelFileStream); ISheet sheet = null; int sheetIndex = -1; if (int.TryParse(sheetName, out sheetIndex)) { sheet = workbook.GetSheetAt(sheetIndex); } else { sheet = workbook.GetSheet(sheetName); } DataTable table = GetDataTableFromSheet(sheet, headerRowIndex); excelFileStream.Close(); workbook = null; sheet = null; return table; } /// <summary> /// 由Excel导入DataTable /// </summary> /// <param name="excelFilePath">Excel文件路径,为物理路径。</param> /// <param name="sheetName">Excel工作表名称</param> /// <param name="headerRowIndex">Excel表头行索引</param> /// <returns>DataTable</returns> public static DataTable ImportFromExcel(string excelFilePath, string sheetName, int headerRowIndex) { using (FileStream stream = System.IO.File.OpenRead(excelFilePath)) { bool isCompatible = GetIsCompatible(excelFilePath); return ImportFromExcel(stream, sheetName, headerRowIndex, isCompatible); } } /// <summary> /// 由Excel导入DataSet,如果有多个工作表,则导入多个DataTable /// </summary> /// <param name="excelFileStream">Excel文件流</param> /// <param name="headerRowIndex">Excel表头行索引</param> /// <param name="isCompatible">是否为兼容模式</param> /// <returns>DataSet</returns> public static DataSet ImportFromExcel(Stream excelFileStream, int headerRowIndex, bool isCompatible) { DataSet ds = new DataSet(); IWorkbook workbook = CreateWorkbook(isCompatible, excelFileStream); for (int i = 0; i < workbook.NumberOfSheets; i++) { ISheet sheet = workbook.GetSheetAt(i); DataTable table = GetDataTableFromSheet(sheet, headerRowIndex); ds.Tables.Add(table); } excelFileStream.Close(); workbook = null; return ds; } /// <summary> /// 由Excel导入DataSet,如果有多个工作表,则导入多个DataTable /// </summary> /// <param name="excelFilePath">Excel文件路径,为物理路径。</param> /// <param name="headerRowIndex">Excel表头行索引</param> /// <returns>DataSet</returns> public static DataSet ImportFromExcel(string excelFilePath, int headerRowIndex) { using (FileStream stream = System.IO.File.OpenRead(excelFilePath)) { bool isCompatible = GetIsCompatible(excelFilePath); return ImportFromExcel(stream, headerRowIndex, isCompatible); } } #endregion #region 公共转换方法 /// <summary> /// 将Excel的列索引转换为列名,列索引从0开始,列名从A开始。如第0列为A,第1列为B... /// </summary> /// <param name="index">列索引</param> /// <returns>列名,如第0列为A,第1列为B...</returns> public static string ConvertColumnIndexToColumnName(int index) { index = index + 1; int system = 26; char[] digArray = new char[100]; int i = 0; while (index > 0) { int mod = index % system; if (mod == 0) mod = system; digArray[i++] = (char)(mod - 1 + 'A'); index = (index - 1) / 26; } StringBuilder sb = new StringBuilder(i); for (int j = i - 1; j >= 0; j--) { sb.Append(digArray[j]); } return sb.ToString(); } /// <summary> /// 转化日期 /// </summary> /// <param name="date">日期</param> /// <returns></returns> public static DateTime ConvertDate(object date) { string dtStr = (date ?? "").ToString(); DateTime dt = new DateTime(); if (DateTime.TryParse(dtStr, out dt)) { return dt; } try { string spStr = ""; if (dtStr.Contains("-")) { spStr = "-"; } else if (dtStr.Contains("/")) { spStr = "/"; } string[] time = dtStr.Split(spStr.ToCharArray()); int year = Convert.ToInt32(time[2]); int month = Convert.ToInt32(time[0]); int day = Convert.ToInt32(time[1]); string years = Convert.ToString(year); string months = Convert.ToString(month); string days = Convert.ToString(day); if (months.Length == 4) { dt = Convert.ToDateTime(date); } else { string rq = ""; if (years.Length == 1) { years = "0" + years; } if (months.Length == 1) { months = "0" + months; } if (days.Length == 1) { days = "0" + days; } rq = "20" + years + "-" + months + "-" + days; dt = Convert.ToDateTime(rq); } } catch { throw new Exception("日期格式不正确,转换日期失败!"); } return dt; } /// <summary> /// 转化数字 /// </summary> /// <param name="d">数字字符串</param> /// <returns></returns> public static decimal ConvertDecimal(object d) { string dStr = (d ?? "").ToString(); decimal result = 0; if (decimal.TryParse(dStr, out result)) { return result; } else { throw new Exception("数字格式不正确,转换数字失败!"); } } #endregion } }
【2.4】文件帮助类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; /* * 除了using System.Web;,你还须在项目中添加引用 程序集 System.Web.dll */ namespace AutomaticStoreMotionDal { /// <summary> /// 文件操作夹 /// </summary> public static class DirFileHelper { #region 打开文件对话框,并返回文件的路径,包含扩展名 /// <summary> /// 打开文件对话框,并返回文件的路径,包含扩展名 /// </summary> /// <returns></returns> public static string GetOpenFilePath() { //创建对话框的对象 OpenFileDialog ofd = new OpenFileDialog(); //设置对话框的标题 ofd.Title = "请选择要打开的文件"; //设置对话框可以多选 ofd.Multiselect = true; //设置对话框的初始目录 ofd.InitialDirectory = @"C:UsersAdministratorDesktop"; //设置对话框打开文件的类型 ofd.Filter = "Excel文件(.xls和.xlsx)|*.xls;*.xlsx"; //展示对话框 ofd.ShowDialog(); //获得在打开对话框中选中的文件的路径 string filePath = ofd.FileName; //全路径 return filePath; } #endregion #region 检测指定目录是否存在 /// <summary> /// 检测指定目录是否存在 /// </summary> /// <param name="directoryPath">目录的绝对路径</param> /// <returns></returns> public static bool IsExistDirectory(string directoryPath) { return Directory.Exists(directoryPath); } #endregion #region 检测指定文件是否存在,如果存在返回true /// <summary> /// 检测指定文件是否存在,如果存在则返回true。 /// </summary> /// <param name="filePath">文件的绝对路径</param> public static bool IsExistFile(string filePath) { return File.Exists(filePath); } #endregion #region 获取指定目录中的文件列表 /// <summary> /// 获取指定目录中所有文件列表 /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> public static string[] GetFileNames(string directoryPath) { //如果目录不存在,则抛出异常 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } //获取文件列表 return Directory.GetFiles(directoryPath); } #endregion #region 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法. /// <summary> /// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法. /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> public static string[] GetDirectories(string directoryPath) { try { return Directory.GetDirectories(directoryPath); } catch (IOException ex) { throw ex; } } #endregion #region 获取指定目录及子目录中所有文件列表 /// <summary> /// 获取指定目录及子目录中所有文件列表 /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。 /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param> /// <param name="isSearchChild">是否搜索子目录</param> public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild) { //如果目录不存在,则抛出异常 if (!IsExistDirectory(directoryPath)) { throw new FileNotFoundException(); } try { if (isSearchChild) { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } #endregion #region 检测指定目录是否为空 /// <summary> /// 检测指定目录是否为空 /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> public static bool IsEmptyDirectory(string directoryPath) { try { //判断是否存在文件 string[] fileNames = GetFileNames(directoryPath); if (fileNames.Length > 0) { return false; } //判断是否存在文件夹 string[] directoryNames = GetDirectories(directoryPath); if (directoryNames.Length > 0) { return false; } return true; } catch { //这里记录日志 //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message); return true; } } #endregion #region 检测指定目录中是否存在指定的文件 /// <summary> /// 检测指定目录中是否存在指定的文件,若要搜索子目录请使用重载方法. /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。 /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param> public static bool Contains(string directoryPath, string searchPattern) { try { //获取指定的文件列表 string[] fileNames = GetFileNames(directoryPath, searchPattern, false); //判断指定文件是否存在 if (fileNames.Length == 0) { return false; } else { return true; } } catch (Exception ex) { throw new Exception(ex.Message); //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message); } } /// <summary> /// 检测指定目录中是否存在指定的文件 /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。 /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param> /// <param name="isSearchChild">是否搜索子目录</param> public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild) { try { //获取指定的文件列表 string[] fileNames = GetFileNames(directoryPath, searchPattern, true); //判断指定文件是否存在 if (fileNames.Length == 0) { return false; } else { return true; } } catch (Exception ex) { throw new Exception(ex.Message); //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message); } } #endregion #region 创建目录 /// <summary> /// 创建目录 /// </summary> /// <param name="dir">要创建的目录路径包括目录名</param> public static void CreateDir(string dir) { if (dir.Length == 0) return; if (!Directory.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir)) Directory.CreateDirectory(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir); } #endregion #region 删除目录 /// <summary> /// 删除目录 /// </summary> /// <param name="dir">要删除的目录路径和名称</param> public static void DeleteDir(string dir) { if (dir.Length == 0) return; if (Directory.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir)) Directory.Delete(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir); } #endregion #region 删除文件 /// <summary> /// 删除文件 /// </summary> /// <param name="file">要删除的文件路径和名称</param> public static void DeleteFile(string file) { if (File.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + file)) File.Delete(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + file); } #endregion #region 创建文件 /// <summary> /// 创建文件 /// </summary> /// <param name="dir">带后缀的文件名</param> /// <param name="pagestr">文件内容</param> public static void CreateFile(string dir, string pagestr) { dir = dir.Replace("/", "\"); if (dir.IndexOf("\") > -1) CreateDir(dir.Substring(0, dir.LastIndexOf("\"))); System.IO.StreamWriter sw = new System.IO.StreamWriter( System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir, false, System.Text.Encoding.GetEncoding("GB2312")); sw.Write(pagestr); sw.Close(); } #endregion #region 移动文件(剪贴--粘贴) /// <summary> /// 移动文件(剪贴--粘贴) /// </summary> /// <param name="dir1">要移动的文件的路径及全名(包括后缀)</param> /// <param name="dir2">文件移动到新的位置,并指定新的文件名</param> public static void MoveFile(string dir1, string dir2) { dir1 = dir1.Replace("/", "\"); dir2 = dir2.Replace("/", "\"); if (File.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir1)) File.Move(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir1, System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir2); } #endregion #region 复制文件 /// <summary> /// 复制文件 /// </summary> /// <param name="dir1">要复制的文件的路径已经全名(包括后缀)</param> /// <param name="dir2">目标位置,并指定新的文件名</param> public static void CopyFile(string dir1, string dir2) { dir1 = dir1.Replace("/", "\"); dir2 = dir2.Replace("/", "\"); if (File.Exists(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir1)) { File.Copy(System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir1, System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "\" + dir2, true); } } #endregion #region 根据时间得到目录名 / 格式:yyyyMMdd 或者 HHmmssff /// <summary> /// 根据时间得到目录名yyyyMMdd /// </summary> /// <returns></returns> public static string GetDateDir() { return DateTime.Now.ToString("yyyyMMdd"); } /// <summary> /// 根据时间得到文件名HHmmssff /// </summary> /// <returns></returns> public static string GetDateFile() { return DateTime.Now.ToString("HHmmssff"); } #endregion #region 复制文件夹 /// <summary> /// 复制文件夹(递归) /// </summary> /// <param name="varFromDirectory">源文件夹路径</param> /// <param name="varToDirectory">目标文件夹路径</param> public static void CopyFolder(string varFromDirectory, string varToDirectory) { Directory.CreateDirectory(varToDirectory); if (!Directory.Exists(varFromDirectory)) return; string[] directories = Directory.GetDirectories(varFromDirectory); if (directories.Length > 0) { foreach (string d in directories) { CopyFolder(d, varToDirectory + d.Substring(d.LastIndexOf("\"))); } } string[] files = Directory.GetFiles(varFromDirectory); if (files.Length > 0) { foreach (string s in files) { File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("\")), true); } } } #endregion #region 检查文件,如果文件不存在则创建 /// <summary> /// 检查文件,如果文件不存在则创建 /// </summary> /// <param name="FilePath">路径,包括文件名</param> public static void ExistsFile(string FilePath) { //if(!File.Exists(FilePath)) //File.Create(FilePath); //以上写法会报错,详细解释请看下文......... if (!File.Exists(FilePath)) { FileStream fs = File.Create(FilePath); fs.Close(); } } #endregion #region 删除指定文件夹对应其他文件夹里的文件 /// <summary> /// 删除指定文件夹对应其他文件夹里的文件 /// </summary> /// <param name="varFromDirectory">指定文件夹路径</param> /// <param name="varToDirectory">对应其他文件夹路径</param> public static void DeleteFolderFiles(string varFromDirectory, string varToDirectory) { Directory.CreateDirectory(varToDirectory); if (!Directory.Exists(varFromDirectory)) return; string[] directories = Directory.GetDirectories(varFromDirectory); if (directories.Length > 0) { foreach (string d in directories) { DeleteFolderFiles(d, varToDirectory + d.Substring(d.LastIndexOf("\"))); } } string[] files = Directory.GetFiles(varFromDirectory); if (files.Length > 0) { foreach (string s in files) { File.Delete(varToDirectory + s.Substring(s.LastIndexOf("\"))); } } } #endregion #region 从文件的绝对路径中获取文件名( 包含扩展名 ) /// <summary> /// 从文件的绝对路径中获取文件名( 包含扩展名 ) /// </summary> /// <param name="filePath">文件的绝对路径</param> public static string GetFileName(string filePath) { if (!string.IsNullOrEmpty(filePath)) { //获取文件的名称 FileInfo fi = new FileInfo(filePath); return fi.Name; } MessageBox.Show("请选择文件"); return ""; } #endregion #region 从文件的绝对路径中获取文件名( 不包含扩展名 ) /// <summary> /// 从文件的绝对路径中获取文件名( 不包含扩展名 ) /// </summary> /// <param name="filePath">文件的绝对路径</param> public static string GetFileNameNoExtension(string filePath) { //获取文件的名称 FileInfo fi = new FileInfo(filePath); return fi.Name.Split('.')[0]; } #endregion #region 从文件的绝对路径中获取扩展名 /// <summary> /// 从文件的绝对路径中获取扩展名 /// </summary> /// <param name="filePath">文件的绝对路径</param> public static string GetExtension(string filePath) { //获取文件的名称 FileInfo fi = new FileInfo(filePath); return fi.Extension; } #region 从文件的绝对路径创建备份的文件路劲(包含扩展名)【另存文件用】 /// <summary> /// 从文件的绝对路径创建备份的文件路劲(包含扩展名)【另存文件用】 /// </summary> /// <param name="filePath">文件的绝对路径</param> public static string GetBackUpFilePathWithFilePath(string filePath,string backUpName) { try { //获取文件所在的目录 string fileDirectory = Path.GetDirectoryName(filePath); //获取文件的名称含有后缀 string fileName = Path.GetFileName(filePath); return fileDirectory + "\" + fileName.Split('.')[0] + backUpName + "." + fileName.Split('.')[1]; } catch (Exception e) { MessageBox.Show("文件不存在: "+e.Message); return null; } } #endregion #endregion /// <summary> /// 复制文件参考方法,页面中引用 /// </summary> /// <param name="cDir">新路径</param> /// <param name="TempId">模板引擎替换编号</param> public static void CopyFiles(string cDir, string TempId) { //if (Directory.Exists(Request.PhysicalApplicationPath + "\Controls")) //{ // string TempStr = string.Empty; // StreamWriter sw; // if (File.Exists(Request.PhysicalApplicationPath + "\Controls\Default.aspx")) // { // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\Controls\Default.aspx"); // TempStr = TempStr.Replace("{$ChannelId$}", TempId); // sw = new StreamWriter(Request.PhysicalApplicationPath + "\" + cDir + "\Default.aspx", false, System.Text.Encoding.GetEncoding("GB2312")); // sw.Write(TempStr); // sw.Close(); // } // if (File.Exists(Request.PhysicalApplicationPath + "\Controls\Column.aspx")) // { // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\Controls\Column.aspx"); // TempStr = TempStr.Replace("{$ChannelId$}", TempId); // sw = new StreamWriter(Request.PhysicalApplicationPath + "\" + cDir + "\List.aspx", false, System.Text.Encoding.GetEncoding("GB2312")); // sw.Write(TempStr); // sw.Close(); // } // if (File.Exists(Request.PhysicalApplicationPath + "\Controls\Content.aspx")) // { // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\Controls\Content.aspx"); // TempStr = TempStr.Replace("{$ChannelId$}", TempId); // sw = new StreamWriter(Request.PhysicalApplicationPath + "\" + cDir + "\View.aspx", false, System.Text.Encoding.GetEncoding("GB2312")); // sw.Write(TempStr); // sw.Close(); // } // if (File.Exists(Request.PhysicalApplicationPath + "\Controls\MoreDiss.aspx")) // { // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\Controls\MoreDiss.aspx"); // TempStr = TempStr.Replace("{$ChannelId$}", TempId); // sw = new StreamWriter(Request.PhysicalApplicationPath + "\" + cDir + "\DissList.aspx", false, System.Text.Encoding.GetEncoding("GB2312")); // sw.Write(TempStr); // sw.Close(); // } // if (File.Exists(Request.PhysicalApplicationPath + "\Controls\ShowDiss.aspx")) // { // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\Controls\ShowDiss.aspx"); // TempStr = TempStr.Replace("{$ChannelId$}", TempId); // sw = new StreamWriter(Request.PhysicalApplicationPath + "\" + cDir + "\Diss.aspx", false, System.Text.Encoding.GetEncoding("GB2312")); // sw.Write(TempStr); // sw.Close(); // } // if (File.Exists(Request.PhysicalApplicationPath + "\Controls\Review.aspx")) // { // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\Controls\Review.aspx"); // TempStr = TempStr.Replace("{$ChannelId$}", TempId); // sw = new StreamWriter(Request.PhysicalApplicationPath + "\" + cDir + "\Review.aspx", false, System.Text.Encoding.GetEncoding("GB2312")); // sw.Write(TempStr); // sw.Close(); // } // if (File.Exists(Request.PhysicalApplicationPath + "\Controls\Search.aspx")) // { // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\Controls\Search.aspx"); // TempStr = TempStr.Replace("{$ChannelId$}", TempId); // sw = new StreamWriter(Request.PhysicalApplicationPath + "\" + cDir + "\Search.aspx", false, System.Text.Encoding.GetEncoding("GB2312")); // sw.Write(TempStr); // sw.Close(); // } //} } #region 创建一个目录 /// <summary> /// 创建一个目录 /// </summary> /// <param name="directoryPath">目录的绝对路径</param> public static void CreateDirectory(string directoryPath) { //如果目录不存在则创建该目录 if (!IsExistDirectory(directoryPath)) { Directory.CreateDirectory(directoryPath); } } #endregion #region 创建一个文件 /// <summary> /// 创建一个文件。 /// </summary> /// <param name="filePath">文件的绝对路径</param> public static void CreateFile(string filePath) { try { //如果文件不存在则创建该文件 if (!IsExistFile(filePath)) { //创建一个FileInfo对象 FileInfo file = new FileInfo(filePath); //创建文件 FileStream fs = file.Create(); //关闭文件流 fs.Close(); } } catch (Exception ex) { //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message); throw ex; } } /// <summary> /// 创建一个文件,并将字节流写入文件。 /// </summary> /// <param name="filePath">文件的绝对路径</param> /// <param name="buffer">二进制流数据</param> public static void CreateFile(string filePath, byte[] buffer) { try { //如果文件不存在则创建该文件 if (!IsExistFile(filePath)) { //创建一个FileInfo对象 FileInfo file = new FileInfo(filePath); //创建文件 FileStream fs = file.Create(); //写入二进制流 fs.Write(buffer, 0, buffer.Length); //关闭文件流 fs.Close(); } } catch (Exception ex) { //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message); throw ex; } } #endregion #region 获取文本文件的行数 /// <summary> /// 获取文本文件的行数 /// </summary> /// <param name="filePath">文件的绝对路径</param> public static int GetLineCount(string filePath) { //将文本文件的各行读到一个字符串数组中 string[] rows = File.ReadAllLines(filePath); //返回行数 return rows.Length; } #endregion #region 获取一个文件的长度 /// <summary> /// 获取一个文件的长度,单位为Byte /// </summary> /// <param name="filePath">文件的绝对路径</param> public static int GetFileSize(string filePath) { //创建一个文件对象 FileInfo fi = new FileInfo(filePath); //获取文件的大小 return (int) fi.Length; } #endregion #region 获取指定目录中的子目录列表 /// <summary> /// 获取指定目录及子目录中所有子目录列表 /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。 /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param> /// <param name="isSearchChild">是否搜索子目录</param> public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild) { try { if (isSearchChild) { return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories); } else { return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly); } } catch (IOException ex) { throw ex; } } #endregion #region 向文本文件写入内容 /// <summary> /// 向文本文件中写入内容 /// </summary> /// <param name="filePath">文件的绝对路径</param> /// <param name="text">写入的内容</param> /// <param name="encoding">编码</param> public static void WriteText(string filePath, string text, Encoding encoding) { //向文件写入内容 File.WriteAllText(filePath, text, encoding); } #endregion #region 向文本文件的尾部追加内容 /// <summary> /// 向文本文件的尾部追加内容 /// </summary> /// <param name="filePath">文件的绝对路径</param> /// <param name="content">写入的内容</param> public static void AppendText(string filePath, string content) { File.AppendAllText(filePath, content); } #endregion #region 将现有文件的内容复制到新文件中 /// <summary> /// 将源文件的内容复制到目标文件中 /// </summary> /// <param name="sourceFilePath">源文件的绝对路径</param> /// <param name="destFilePath">目标文件的绝对路径</param> public static void Copy(string sourceFilePath, string destFilePath) { File.Copy(sourceFilePath, destFilePath, true); } #endregion #region 将文件移动到指定目录 /// <summary> /// 将文件移动到指定目录 /// </summary> /// <param name="sourceFilePath">需要移动的源文件的绝对路径</param> /// <param name="descDirectoryPath">移动到的目录的绝对路径</param> public static void Move(string sourceFilePath, string descDirectoryPath) { //获取源文件的名称 string sourceFileName = GetFileName(sourceFilePath); if (IsExistDirectory(descDirectoryPath)) { //如果目标中存在同名文件,则删除 if (IsExistFile(descDirectoryPath + "\" + sourceFileName)) { DeleteFile(descDirectoryPath + "\" + sourceFileName); } //将文件移动到指定目录 File.Move(sourceFilePath, descDirectoryPath + "\" + sourceFileName); } } #endregion #region 清空指定目录 /// <summary> /// 清空指定目录下所有文件及子目录,但该目录依然保存. /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> public static void ClearDirectory(string directoryPath) { if (IsExistDirectory(directoryPath)) { //删除目录中所有的文件 string[] fileNames = GetFileNames(directoryPath); for (int i = 0; i < fileNames.Length; i++) { DeleteFile(fileNames[i]); } //删除目录中所有的子目录 string[] directoryNames = GetDirectories(directoryPath); for (int i = 0; i < directoryNames.Length; i++) { DeleteDirectory(directoryNames[i]); } } } #endregion #region 清空文件内容 /// <summary> /// 清空文件内容 /// </summary> /// <param name="filePath">文件的绝对路径</param> public static void ClearFile(string filePath) { //删除文件 File.Delete(filePath); //重新创建该文件 CreateFile(filePath); } #endregion #region 删除指定目录 /// <summary> /// 删除指定目录及其所有子目录 /// </summary> /// <param name="directoryPath">指定目录的绝对路径</param> public static void DeleteDirectory(string directoryPath) { if (IsExistDirectory(directoryPath)) { Directory.Delete(directoryPath, true); } } #endregion /* string filePath = "C:\JiYF\BenXH\BenXHCMS.xml"; Console.WriteLine("该文件的目录:"+filePath); string str = "获取文件的全路径:" + Path.GetFullPath(filePath); //-->C:JiYFBenXHBenXHCMS.xml str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath); //-->C:JiYFBenXH str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath); //-->BenXHCMS.xml str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath); //-->BenXHCMS str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath); //-->.xml str = "获取路径的根目录:" + Path.GetPathRoot(filePath); //-->C: */ } }