推荐文章

Winform 全屏显示

Winform 全屏显示

Winform程序无边框、置顶、全屏显示
WPF之Image控件

WPF之Image控件

Image控件加载图片包括加载动态图片,加载静态图片两种方式。加载动态图片通过生成一个BitmapImage,创建该对象后,赋给Image的Source即可。加载的形式:BitmapImage myBitmapImage =new BitmapImage();myBitmapImage.BeginInit();取得数据库存储的图片字段,MSSQL的Image类型Byte[] mybyte = Re
wpf list<T>与ObservableCollection<T>的区别

wpf list<T>与ObservableCollection<T>的区别

List《T》与ObservableCollection《T》的用法基本上是一样的。区别:list《T》:当T继承于INotifyPropertyChanged时,如果list《T》中的属性发生改变,则通知UI属性值已发生改变。但当list《T》添加一项时,list《T》就无法通知前端UI了(此时,ObservableCollection《T》闪亮登场)。ObservableCollection《
WPF调用线程复制文件并显示进度条

WPF调用线程复制文件并显示进度条

在WPF中复制文件时,如何调用线程,显示进度条。《Window x:Class="WpfThreadTest.MainWindow"xmlns="http:schemas.microsoft.comwinfx2006xamlpresentation"xmlns:x="http:schemas.microsoft.comwinfx2006xaml"Title="MainWindow" Closed=
WPF中对XML的读写

WPF中对XML的读写

已知有一个XML文件(bookste.xml)如下:《?xmlversion="1.0" encoding="gb2312"?》《bookste》《bookgenre="fantasy" ISBN="236314"》《title》OberonsLegacy《title》《auth》Cets,Eva《auth》《price》5.95《price》《book》《bookste》 1、往《bookste

WPF通过NPIO读写Excel操作

日期:2018-08-17 点击:1717 来源:PB2.CN

NPOITools

public class NPOITools
    {
        #region 打开保存excel对话框返回文件名
       public static string SaveExcelFileDialog()
        {
            var sfd = new Microsoft.Win32.SaveFileDialog()
            {
                DefaultExt = "xls",
                Filter = "excel files(*.xls)|*.xls|All files(*.*)|*.*",
                FilterIndex = 1
            };
            if (sfd.ShowDialog() != true)
                return null;
            return sfd.FileName;
        }
        #endregion
        #region 打开excel对话框返回文件名
        public static string OpenExcelFileDialog()
        {
            var ofd = new Microsoft.Win32.OpenFileDialog()
            {
                DefaultExt = "xls",
                Filter = "excel files(*.xls)|*.xls|All files(*.*)|*.*",
                FilterIndex = 1
            };
            if (ofd.ShowDialog() != true)
                return null;
            return ofd.FileName;
        }
        #endregion
        #region 读excel
        public static DataTable ImportExcelFile()
        {
            DataTable dt = new DataTable();
            //打开excel对话框
            var filepath = OpenExcelFileDialog();
            if (filepath != null)
            {
                HSSFWorkbook hssfworkbook = null;
                #region//初始化信息
                try
                {
                    using (FileStream file = new FileStream(filepath, FileMode.Open, FileAccess.Read))
                    {
                        hssfworkbook = new HSSFWorkbook(file);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
                #endregion
                var sheet = hssfworkbook.GetSheetAt(0);
                System.Collections.IEnumerator rows = sheet.GetRowEnumerator();
             
                for (int j = 0; j < (sheet.GetRow(0).LastCellNum); j++)
                {
                    dt.Columns.Add(Convert.ToChar(((int)'A') + j).ToString());
                }
                while (rows.MoveNext())
                {
                    HSSFRow row = (HSSFRow)rows.Current;
                    DataRow dr = dt.NewRow();
                    for (int i = 0; i < row.LastCellNum; i++)
                    {
                        var cell = row.GetCell(i);
                        if (cell == null)
                        {
                            dr[i] = "";
                        }
                        else
                        {
                          if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                            {
                                if (HSSFDateUtil.IsCellDateFormatted(cell))
                                {
                                    dr[i] = cell.DateCellValue;
                                }
                                else
                                {
                                    dr[i] = cell.NumericCellValue;
                                }
                            }
                            else if (cell.CellType == NPOI.SS.UserModel.CellType.Boolean)
                            {
                                dr[i] = cell.BooleanCellValue;
                            }
                            else
                            {
                                dr[i] = cell.StringCellValue;
                            }
                        }
                    }
                    dt.Rows.Add(dr);
                }
            }
            return dt;
        }
        #endregion
     
        #region list转datatable
        public static DataTable ListToDataTable<T>(IEnumerable<T> c)
        {
            var props = typeof(T).GetProperties();
            var dt = new DataTable();
            dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (c.Count() > 0)
            {
                for (int i = 0; i < c.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo item in props)
                    {
                        object obj = item.GetValue(c.ElementAt(i), null);
                        tempList.Add(obj);
                    }
                    dt.LoadDataRow(tempList.ToArray(), true);
                }
            }
            return dt;
        }
        #endregion
        #region 写入excel
        public static bool WriteExcel<T>(IList<T> list)
        {
            //打开保存excel对话框
            var filepath = SaveExcelFileDialog();
            if (filepath == null)
                return false;
            var dt = ListToDataTable<T>(list);
            if (!string.IsNullOrEmpty(filepath) && null != dt && dt.Rows.Count > 0)
            {
                NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
                NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("Sheet1");
                NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    row.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(i + 1);
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        row2.CreateCell(j).SetCellValue(Convert.ToString(dt.Rows[i][j]));
                    }
                }
                // 写入到客户端 
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    book.Write(ms);
                    using (FileStream fs = new FileStream(filepath, FileMode.Create, FileAccess.Write))
                    {
                        byte[] data = ms.ToArray();
                        fs.Write(data, 0, data.Length);
                        fs.Flush();
                    }
                    book = null;
                }
            }
            return true;
        }
        #endregion
    }


这篇文档对您是否有帮助?

Winform 全屏显示

Winform 全屏显示

Winform程序无边框、置顶、全屏显示
WPF之Image控件

WPF之Image控件

Image控件加载图片包括加载动态图片,加载静态图片两种方式。加载动态图片通过生成一个BitmapImage,创建该对象后,赋给Image的Source即可。加载的形式:BitmapImage myBitmapImage =new BitmapImage();myBitmapImage.BeginInit();取得数据库存储的图片字段,MSSQL的Image类型Byte[] mybyte = Re
wpf list<T>与ObservableCollection<T>的区别

wpf list<T>与ObservableCollection<T>的区别

List《T》与ObservableCollection《T》的用法基本上是一样的。区别:list《T》:当T继承于INotifyPropertyChanged时,如果list《T》中的属性发生改变,则通知UI属性值已发生改变。但当list《T》添加一项时,list《T》就无法通知前端UI了(此时,ObservableCollection《T》闪亮登场)。ObservableCollection《
WPF调用线程复制文件并显示进度条

WPF调用线程复制文件并显示进度条

在WPF中复制文件时,如何调用线程,显示进度条。《Window x:Class="WpfThreadTest.MainWindow"xmlns="http:schemas.microsoft.comwinfx2006xamlpresentation"xmlns:x="http:schemas.microsoft.comwinfx2006xaml"Title="MainWindow" Closed=
WPF中对XML的读写

WPF中对XML的读写

已知有一个XML文件(bookste.xml)如下:《?xmlversion="1.0" encoding="gb2312"?》《bookste》《bookgenre="fantasy" ISBN="236314"》《title》OberonsLegacy《title》《auth》Cets,Eva《auth》《price》5.95《price》《book》《bookste》 1、往《bookste