c# Excel数据的导出与导入

搬运:Datagrideview 数据导出Excel  , Exel数据导入

//-------------------------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2013 , DZD , Ltd .
//-------------------------------------------------------------------------------------

using System;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using NPOI.HPSF;
using NPOI.HSSF.UserModel;
using NPOI.HSSF.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace LineSet
{
    /// 
    /// Excel操作类,这里的NPOI使用的是2.0.1 (beta1)版本的
    ///
    /// 修改纪录
    ///
    ///		2013-10-25 版本:1.0 yanghenglian 创建主键,注意命名空间的排序。
    ///		2013-11-01 版本:1.0 yanghenglian 添加了支持B/S的导出和导入以及C/S的导出和导入功能,代码感觉可以在优化,开心。
    ///		2014-01-16 版本:1.0 yanghenglian 添加了支持xlsxExcel文件读取判断
    /// 
    /// 版本:1.0
    ///
    /// 
    ///		yanghenglian
    ///		2013-11-01
    /// 
    /// 
    public class ExcelHelper
    {
        private static HSSFWorkbook _hssfworkbook;
        public static string EroMsg = "";
        /********************************************************下面的五个是最终版本适合B/S和C/S*****************************************************/

        #region ExcelToDataTable(string strExcelFileName, string strSheetName) Excel转换成DataTable--B/S和C/S都可以使用

        /// 
        /// Excel转换成DataTable
        /// 
        /// 文件路径
        /// Excel中对应的sheet表单名称,如:sheet1,sheet2
        /// 数据集
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2100:检查 SQL 查询是否存在安全漏洞")]
        public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
        {
            string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + strExcelFileName + ";" +
                             "Extended Properties=Excel 5.0;";
            string strExcel = string.Format("select * from [{0}$]", strSheetName);
            DataSet ds = new DataSet();
            using (OleDbConnection conn = new OleDbConnection(strConn))
            {
                conn.Open();
                OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
                adapter.Fill(ds, strSheetName);
                conn.Close();
                return ds.Tables[strSheetName];
            }
        }

        #endregion

        #region DataTable ExcelToDataTable(string strFileName, int sheetIndex = 0) 根据索引读取Sheet表数据,默认读取第一个sheet--B/S和C/S都可以使用

        /// 读取excel
        /// 根据索引读取Sheet表数据,默认读取第一个sheet
        /// 
        /// excel文档路径
        /// sheet表的索引,从0开始
        /// 数据集
        public static DataTable ExcelToDataTable(string strFileName, int sheetIndex = 0)
        {
            DataTable dt = new DataTable();
            HSSFWorkbook hssfworkbook = null;
            XSSFWorkbook xssfworkbook = null;
            string fileExt = Path.GetExtension(strFileName);//获取文件的后缀名
            try
            {
                using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read))
                {
                    if (fileExt.ToLower() == ".xls")
                        hssfworkbook = new HSSFWorkbook(file);
                    else if (fileExt.ToLower() == ".xlsx")
                        xssfworkbook = new XSSFWorkbook(file);//初始化太慢了,不知道这是什么bug
                }
            }
            catch (Exception ex)
            {
                EroMsg = ex.Message;
                return null;
            }

            if (hssfworkbook != null)
            {
                HSSFSheet sheet = (HSSFSheet)hssfworkbook.GetSheetAt(sheetIndex);
                if (sheet != null)
                {
                    System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
                    HSSFRow headerRow = (HSSFRow)sheet.GetRow(0);
                    int cellCount = headerRow.LastCellNum;
                    for (int j = 0; j < cellCount; j++)
                    {
                        HSSFCell cell = (HSSFCell)headerRow.GetCell(j);
                        dt.Columns.Add(cell.ToString());
                    }
                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
                    {
                        HSSFRow row = (HSSFRow)sheet.GetRow(i);
                        DataRow dataRow = dt.NewRow();
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            if (row.GetCell(j) != null)
                                dataRow[j] = row.GetCell(j).ToString();
                        }
                        dt.Rows.Add(dataRow);
                    }
                }
            }
            else if (xssfworkbook != null)
            {
                XSSFSheet xSheet = (XSSFSheet)xssfworkbook.GetSheetAt(sheetIndex);
                if (xSheet != null)
                {
                    System.Collections.IEnumerator rows = xSheet.GetRowEnumerator();
                    XSSFRow headerRow = (XSSFRow)xSheet.GetRow(0);
                    int cellCount = headerRow.LastCellNum;
                    for (int j = 0; j < cellCount; j++)
                    {
                        XSSFCell cell = (XSSFCell)headerRow.GetCell(j);
                        dt.Columns.Add(cell.ToString());
                    }
                    for (int i = (xSheet.FirstRowNum + 1); i <= xSheet.LastRowNum; i++)
                    {
                        XSSFRow row = (XSSFRow)xSheet.GetRow(i);
                        DataRow dataRow = dt.NewRow();
                        for (int j = row.FirstCellNum; j < cellCount; j++)
                        {
                            if (row.GetCell(j) != null)
                                dataRow[j] = row.GetCell(j).ToString();
                        }
                        dt.Rows.Add(dataRow);
                    }
                }
            }
            return dt;
        }

        #endregion

        #region DataGridViewToExcel(DataGridView myDgv, string strHeaderText, string strFileName) DataGridView导出到Excel文件--C/S

        /// 
        /// C/S Winform中DataGridView导出数据到Excel
        /// 
        /// DataGridView控件名称
        /// 保存的文件名称,默认没有,调用的时候最好加上,中英文都支持
        /// 导出后是否打开文件和所在文件夹
        /// 默认保存在“我的文档”中,可自定义保存的文件夹路径
        /// Excel中第一行的标题文字,默认没有,可以自定义
        /// Excel中列名的数组,默认绑定GridView的列名
        public static void DataGridViewToExcel(DataGridView myDgv, string saveFileName = null, bool isOpen = false,
            string saveFilePath = null, string strHeaderText = null, string[] titleNames = null)
        {

            string na = saveFileName.Split('.')[0];
            string kzm = saveFileName.Split('.')[1];
            using (MemoryStream ms = DataGridViewToExcel(myDgv, strHeaderText, titleNames))
            {
                if (string.IsNullOrEmpty(saveFileName)) //文件名为空
                {
                    saveFileName = DateTime.Now.Ticks.ToString();
                }
                if (string.IsNullOrEmpty(saveFilePath) || !System.IO.Directory.Exists(saveFilePath)) //保存路径为空或者不存在
                {
                    saveFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //默认在文档文件夹中
                }
                string saveFullPath = saveFilePath + "\\" + saveFileName;
                //if (System.IO.File.Exists(saveFullPath)) //验证文件重复性
                //{
                //    saveFullPath = saveFilePath + "\\" + na +DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(":", "-").Replace(" ", "-") +"."+ kzm;
                //}
                using (FileStream fs = new FileStream(saveFullPath, FileMode.Create, FileAccess.Write))
                {
                    byte[] data = ms.ToArray();
                    fs.Write(data, 0, data.Length);
                    fs.Flush();
                }
                if (isOpen)
                {
                    Process.Start(saveFullPath); //打开文件
                    Process.Start(saveFilePath); //打开文件夹
                }
            }
        }

        #endregion

        /********************************************************分割线往下方法不用与导入导出*********************************************************/

        #region DataGridViewToExcel(DataGridView myDgv, string strHeaderText) DataTable导出到Excel的MemoryStream

        /// 
        /// DataTable导出到Excel的MemoryStream
        /// 
        /// DataGridView控件名称
        /// 第一行标题头
        /// 列名称数组
        /// 内存流
        private static MemoryStream DataGridViewToExcel(DataGridView myDgv, string strHeaderText = null,
            string[] titleNames = null)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();

            #region 右击文件 属性信息

            {
                DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                dsi.Company = "NPOI";
                workbook.DocumentSummaryInformation = dsi;

                SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
                si.Author = "文件作者信息"; //填加xls文件作者信息
                si.ApplicationName = "创建程序信息"; //填加xls文件创建程序信息
                si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息
                si.Comments = "作者信息"; //填加xls文件作者信息
                si.Title = "标题信息"; //填加xls文件标题信息
                si.Subject = "主题信息"; //填加文件主题信息
                si.CreateDateTime = System.DateTime.Now;
                workbook.SummaryInformation = si;
            }

            #endregion

            HSSFCellStyle dateStyle = (HSSFCellStyle)workbook.CreateCellStyle();
            HSSFDataFormat format = (HSSFDataFormat)workbook.CreateDataFormat();
            dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");

            //取得列宽
            int id = 0;
            foreach (DataGridViewColumn item in myDgv.Columns)
            {
                if (item.Visible == true)
                {
                    id++;
                }
            }
            int[] arrColWidth = new int[id];
            id = 0;
            foreach (DataGridViewColumn item in myDgv.Columns)
            {
                if (item.Visible == true)
                {
                    arrColWidth[id] = Encoding.GetEncoding(936).GetBytes(item.HeaderText).Length;
                    id++;
                }
            }

            for (int i = 0; i < myDgv.Rows.Count; i++)
            {
                id = 0;
                for (int j = 0; j < myDgv.Columns.Count; j++)
                {
                    if (myDgv.Columns[j].Visible == true)
                    {
                        int intTemp = Encoding.GetEncoding(936).GetBytes(myDgv.Rows[i].Cells[j].ToString()).Length;
                        if (intTemp > arrColWidth[id])
                        {
                            arrColWidth[id] = intTemp;
                        }
                        id++;
                    }
                }
            }
            int rowIndex = 0;
            foreach (DataGridViewRow row in myDgv.Rows)
            {
                #region 新建表,填充表头,填充列头,样式

                if (rowIndex == 65535 || rowIndex == 0)
                {
                    if (rowIndex != 0)
                    {
                        sheet = (HSSFSheet)workbook.CreateSheet();
                    }

                    #region 表头及样式

                    {
                        if (!string.IsNullOrEmpty(strHeaderText))
                        {
                            HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);
                            headerRow.HeightInPoints = 25;
                            headerRow.CreateCell(0).SetCellValue(strHeaderText);

                            HSSFCellStyle headStyle = (HSSFCellStyle)workbook.CreateCellStyle();
                            //  headStyle.Alignment = CellHorizontalAlignment.CENTER;
                            HSSFFont font = (HSSFFont)workbook.CreateFont();
                            font.FontHeightInPoints = 20;
                            font.IsBold = true;
                            font.FontName = "宋体";
                            headStyle.SetFont(font);
                            headerRow.GetCell(0).CellStyle = headStyle;
                            // sheet.AddMergedRegion(new Region(0, 0, 0, dtSource.Columns.Count - 1));
                            //headerRow.Dispose();
                            rowIndex++;
                        }
                    }

                    #endregion


                    #region 列头及样式
                    {
                        HSSFRow headerRow = (HSSFRow)sheet.CreateRow(rowIndex);
                        HSSFCellStyle headStyle = (HSSFCellStyle)workbook.CreateCellStyle();
                        // headStyle.Alignment = CellHorizontalAlignment.CENTER;
                        HSSFFont font = (HSSFFont)workbook.CreateFont();
                        font.FontHeightInPoints = 14;
                        font.IsBold = true;
                        font.FontName = "宋体";
                        headStyle.SetFont(font);
                        if (titleNames != null)
                        {
                            if (titleNames.Length > 0)
                            {
                                for (int i = 0; i < titleNames.Length; i++)
                                {
                                    headerRow.CreateCell(i).SetCellValue(titleNames[i]);
                                    headerRow.GetCell(i).CellStyle = headStyle;
                                    //设置列宽
                                    sheet.SetColumnWidth(i, (arrColWidth[i] + 1) * 100);
                                }
                            }
                        }
                        else
                        {
                            id = 0;
                            foreach (DataGridViewColumn column in myDgv.Columns)
                            {
                                if (column.Visible)
                                {
                                    headerRow.CreateCell(id).SetCellValue(column.HeaderText);
                                    headerRow.GetCell(id).CellStyle = headStyle;
                                    //设置列宽
                                    sheet.SetColumnWidth(id, (arrColWidth[id] + 1) * 100);
                                    id++;
                                }

                            }
                        }
                        rowIndex++;
                        // headerRow.Dispose();
                    }

                    #endregion
                }

                #endregion

                #region 填充内容

                HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
                if (row.Index >= 0)
                {
                    bool rel = false;
                    id = 0;
                    foreach (DataGridViewColumn column in myDgv.Columns)
                    {
                        if (column.Visible)
                        {
                            HSSFCell newCell = (HSSFCell)dataRow.CreateCell(id);
                            string drValue = "";
                            if (myDgv.Rows[row.Index].Cells[column.Index].Value != null)
                            {
                                drValue = myDgv.Rows[row.Index].Cells[column.Index].Value.ToString();
                            }
                            string type = "System.String";
                            if (column.ValueType != null)
                            {
                                type = column.ValueType.ToString();
                            }
                            switch (type)
                            {
                                case "System.String": //字符串类型
                                    newCell.SetCellValue(drValue);
                                    break;
                                case "System.DateTime": //日期类型
                                    System.DateTime dateV;
                                    rel = System.DateTime.TryParse(drValue, out dateV);
                                    if (rel)
                                    {
                                        newCell.SetCellValue(dateV);
                                    }
                                    else
                                    {
                                        newCell.SetCellValue("");
                                    }

                                    newCell.CellStyle = dateStyle; //格式化显示
                                    break;
                                case "System.Boolean": //布尔型
                                    bool boolV = false;
                                    rel = bool.TryParse(drValue, out boolV);
                                    if (rel)
                                    {
                                        newCell.SetCellValue(boolV);
                                    }
                                    else
                                    {
                                        newCell.SetCellValue("");
                                    }
                                    break;
                                case "System.Int16": //整型
                                case "System.Int32":
                                case "System.Int64":
                                case "System.Byte":
                                    int intV = 0;
                                    rel = int.TryParse(drValue, out intV);
                                    if (rel)
                                    {
                                        newCell.SetCellValue(intV);
                                    }
                                    else
                                    {
                                        newCell.SetCellValue("");
                                    }
                                    break;
                                case "System.Decimal": //浮点型
                                case "System.Double":
                                    double doubV = 0;
                                    rel = double.TryParse(drValue, out doubV);
                                    if (rel)
                                    {
                                        newCell.SetCellValue(doubV);
                                    }
                                    else
                                    {
                                        newCell.SetCellValue("");
                                    }
                                    break;
                                case "System.DBNull": //空值处理
                                    newCell.SetCellValue("");
                                    break;
                                default:
                                    newCell.SetCellValue("");
                                    break;
                            }
                            id++;
                        }
                    }
                }
                else
                {
                    rowIndex--;
                }

                #endregion

                rowIndex++;
            }
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                //    sheet.Dispose();
                //workbook.Dispose();//一般只用写这一个就OK了,他会遍历并释放所有资源,但当前版本有问题所以只释放sheet
                return ms;
            }
        }

        #endregion

        #region GetExcelStream() 工作簿中的数据流写入到根目录

        /// 
        /// 写入到内存流中
        /// 
        /// 
        private static MemoryStream GetExcelStream()
        {
            //Write the stream data of workbook to the root directory--工作簿中的数据流写入到根目录
            MemoryStream file = new MemoryStream();
            _hssfworkbook.Write(file);
            return file;
        }

        #endregion

        #region GenerateData(DataTable sourceDt, string[] titleNames, string strHeadName, string sheetName = "Sheet1") 构建数据集,每一个Sheet表

        /// 
        /// 构建数据集,每一个Sheet表
        /// 
        /// 数据源
        /// 列名
        /// 第一行显示标题
        /// 需要创建的表的名称,默认是Sheet1
        /// 是否打开文件
        private static void GenerateData(DataTable sourceDt, string[] titleNames, string strHeadName,
            string sheetName = "Sheet1")
        {
            ISheet sheet1 = _hssfworkbook.CreateSheet(sheetName);
            int rowIndex = 0;
            if (!string.IsNullOrEmpty(strHeadName)) //头部大标题
            {
                sheet1.CreateRow(rowIndex).CreateCell(0).SetCellValue(strHeadName);
                rowIndex++;
            }
            IRow rowSecond = sheet1.CreateRow(rowIndex);
            if (titleNames != null) //构建自定义列标题
            {
                if (titleNames.Length > 0)
                {
                    for (int k = 0; k < titleNames.Length; k++)
                    {
                        var cell = rowSecond.CreateCell(k);
                        cell.SetCellValue(titleNames[k]);
                        cell.CellStyle = Getcellstyle(_hssfworkbook, stylexls.头);
                    }
                    rowIndex++;
                }
            }
            else //构建DataTable的列标题
            {
                if (sourceDt.Columns.Count > 0)
                {
                    for (int i = 0; i < sourceDt.Columns.Count; i++)
                    {
                        var cell = rowSecond.CreateCell(i);
                        cell.SetCellValue(sourceDt.Columns[i].ColumnName);
                        cell.CellStyle = Getcellstyle(_hssfworkbook, stylexls.头);
                    }
                    rowIndex++;
                }
            }

            if (sourceDt != null && sourceDt.Rows.Count > 0)
            {
                for (int i = 0; i < sourceDt.Rows.Count; i++)
                {
                    IRow row = sheet1.CreateRow(rowIndex++);
                    for (int j = 0; j < sourceDt.Columns.Count; j++)
                    {
                        row.CreateCell(j).SetCellValue(sourceDt.Rows[i][j].ToString());
                    }
                }
            }
        }

        #endregion

        #region InitializeWorkbook() 导出文件的属性详细信息

        /// 
        /// 导出文件的属性详细信息
        /// 
        private static void InitializeWorkbook()
        {
            _hssfworkbook = new HSSFWorkbook();

            #region 右击文件 属性信息

            {
                DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                dsi.Company = "DZD";
                _hssfworkbook.DocumentSummaryInformation = dsi;

                SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
                si.Author = "文件作者信息"; //填加xls文件作者信息
                si.ApplicationName = "创建程序信息"; //填加xls文件创建程序信息
                si.LastAuthor = "最后保存者信息"; //填加xls文件最后保存者信息
                si.Comments = "作者信息"; //填加xls文件作者信息
                si.Title = "标题信息"; //填加xls文件标题信息
                si.Subject = "主题信息"; //填加文件主题信息
                si.CreateDateTime = DateTime.Now;
                _hssfworkbook.SummaryInformation = si;
            }

            #endregion
        }

        #endregion

        #region 定义单元格常用到样式

        private static ICellStyle Getcellstyle(IWorkbook wb, stylexls str)
        {
            ICellStyle cellStyle = wb.CreateCellStyle();

            //定义几种字体  
            //也可以一种字体,写一些公共属性,然后在下面需要时加特殊的  
            IFont font12 = wb.CreateFont();
            font12.FontHeightInPoints = 14;
            font12.FontName = "宋体";


            IFont font = wb.CreateFont();
            font.FontName = "宋体";
            //font.Underline = 1;下划线  


            IFont fontcolorblue = wb.CreateFont();
            fontcolorblue.Color = HSSFColor.OliveGreen.Blue.Index;
            fontcolorblue.IsItalic = true; //下划线  
            fontcolorblue.FontName = "宋体";


            //边框  
            cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Dotted;
            cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Hair;
            cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Hair;
            cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Dotted;
            //边框颜色  
            cellStyle.BottomBorderColor = HSSFColor.OliveGreen.Blue.Index;
            cellStyle.TopBorderColor = HSSFColor.OliveGreen.Blue.Index;

            //背景图形,我没有用到过。感觉很丑  
            //   cellStyle.FillBackgroundColor = HSSFColor.OLIVE_GREEN.GREEN.index;
            //cellStyle.FillForegroundColor = HSSFColor.OLIVE_GREEN.BLUE.index;  
            // cellStyle.FillForegroundColor = HSSFColor.WHITE.index;
            // cellStyle.FillPattern = FillPatternType.NO_FILL;  
            cellStyle.FillBackgroundColor = HSSFColor.Maroon.Index;

            //水平对齐  
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Left;

            //垂直对齐  
            cellStyle.VerticalAlignment = VerticalAlignment.Center;

            //自动换行  
            // cellStyle.WrapText = true;

            //缩进;当设置为1时,前面留的空白太大了。希旺官网改进。或者是我设置的不对  
            cellStyle.Indention = 0;

            //上面基本都是设共公的设置  
            //下面列出了常用的字段类型  
            switch (str)
            {
                case stylexls.头:
                    // cellStyle.FillPattern = FillPatternType.LEAST_DOTS;  
                    cellStyle.SetFont(font12);
                    break;
                case stylexls.时间:
                    IDataFormat datastyle = wb.CreateDataFormat();

                    cellStyle.DataFormat = datastyle.GetFormat("yyyy/mm/dd");
                    cellStyle.SetFont(font);
                    break;
                case stylexls.数字:
                    cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00");
                    cellStyle.SetFont(font);
                    break;
                case stylexls.钱:
                    IDataFormat format = wb.CreateDataFormat();
                    cellStyle.DataFormat = format.GetFormat("¥#,##0");
                    cellStyle.SetFont(font);
                    break;
                case stylexls.url:
                    fontcolorblue.Underline = FontUnderlineType.Single;
                    cellStyle.SetFont(fontcolorblue);
                    break;
                case stylexls.百分比:
                    cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00%");
                    cellStyle.SetFont(font);
                    break;
                case stylexls.中文大写:
                    IDataFormat format1 = wb.CreateDataFormat();
                    cellStyle.DataFormat = format1.GetFormat("[DbNum2][$-804]0");
                    cellStyle.SetFont(font);
                    break;
                case stylexls.科学计数法:
                    cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0.00E+00");
                    cellStyle.SetFont(font);
                    break;
                case stylexls.默认:
                    cellStyle.SetFont(font);
                    break;
            }
            return cellStyle;
        }

        #endregion

        #region 定义单元格常用到样式的枚举

        public enum stylexls
        {
            头,
            url,
            时间,
            数字,
            钱,
            百分比,
            中文大写,
            科学计数法,
            默认
        }

        #endregion
    }
}

使用的Npoi  

需要引用相关的dll文件

使用方法

导出:

        private void button3_Click(object sender, EventArgs e)
        {
            DgvToExcel(dgv_1);
        }

  private void DgvToExcel(DataGridView dgv)
        {
            if (dgv.Rows.Count == 0)
            {

                MessageBox.Show("无可导出信息!");
                return;
            }
            SaveFileDialog ofd = new SaveFileDialog();//;*.xlsx
             //打开的文件选择对话框上的标题
            ofd.Title = "请选择文件";
            //设置文件类型
            ofd.Filter = "Excel (*.xls)|*.xls";
            //设置默认文件类型显示顺序
            ofd.FilterIndex = 1;
            //保存对话框是否记忆上次打开的目录
            ofd.RestoreDirectory = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string localFilePath = ofd.FileName.ToString();
                string filePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));
                string fileName = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
                try
                {
                    ExcelHelper.DataGridViewToExcel(dgv, fileName, false, filePath, null, null);
                    MessageBox.Show("导出完毕!");

                }
                catch (Exception ex)
                {
                    MessageBox.Show(@"导出失败!" + ex.ToString());
                }
            }
        }

导入:

  private void bt_InDate_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "数据文件(CSV,xls,xlsx)  |*.CSV;*.xls;*.xlsx";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string kzm = openFileDialog.SafeFileName.Split('.')[1].ToLower();

                if (kzm == "csv")
                {
                    CsvHelp csv = new CsvHelp();
                    DataTable dt = csv.CSV2DataTable(openFileDialog.FileName, false);
                    Dgv_2.DataSource = null;
                    Dgv_2.DataSource = dt;
                }
                else if (kzm == "xls" || kzm == "xlsx")
                {
                    DataTable dt = ExcelHelper.ExcelToDataTable(openFileDialog.FileName);
                    if (dt == null)
                    {
                        Form1.mt.Show(ExcelHelper.EroMsg, true);
                        return;
                    }
                    Dgv_2.DataSource = null;
                    Dgv_2.DataSource = dt;
                }
}

你可能感兴趣的:(c#)