利用NPOI和List泛型导出Excel

具体方法不要看,这里这是一个参考

//弹出表的保存路径

private void Output_Click_1(object sender,RoutedEventArgs e)//导出方式合为一个方法体(强制转换sender)

       {

           if (Grid_Center.ItemsSource != null)

           {

               DevExpress.Xpf.Bars.BarButtonItem s =(DevExpress.Xpf.Bars.BarButtonItem)sender;

                string name = s.Name;

                Microsoft.Win32.SaveFileDialogsave = new Microsoft.Win32.SaveFileDialog();//有没有只取路径的控件?貌似没有

                ActionResult ar = newActionResult(true, "");

                switch (name)

                {

                    case"bbiOutputXls":

                        save.Filter ="*.xls|.xls";

                        save.FileName = "文件名";

                        save.Title ="Export To Excel";

                        break;

                    case"bbiOutputXls_1":

                        save.Filter ="*.xls|.xls";

                        save.FileName = "文件名";

                        save.Title ="Export To Excel";

                        break;

                    case "bbiOutputCsv":

                        save.Filter ="*.csv|.csv";

                        save.FileName = "文件名";

                        save.Title ="Export To Csv";

                        break;

                    default:

                        break;

                }

                bool? result =save.ShowDialog();

                if (result.Value)//点击保存

                {

                    string path =save.FileName;

                    try

                    {

                        switch (name)

                        {

                            case"bbiOutputXls":

                               //DevExpress.XtraPrinting.XlsExportOptions options = newDevExpress.XtraPrinting.XlsExportOptions();

 

                                //GridView_Center.ExportToXls(path,options);

                               ar=OutToExcel(path,1);

                                break;

                            case"bbiOutputXls_1"://导出表

                               ar=OutToExcel(path,2);

                                break;

                            case"bbiOutputCsv":

                               GridView_Center.ExportToCsv(path);

                                break;

                            default:

                                break;

                        }

                        if (ar.IsSucceeded)

                        {

                           TopMessageBox.Show(TopDialogType.Tip, MessageBoxString.导出成功);

                        }

                        else

                        {

                           TopMessageBox.Show(TopDialogType.Tip, MessageBoxString.导出失败+ar.Message);

                        }

                    }

                    catch

                    {

                        TopMessageBox.Show(TopDialogType.Wrong,MessageBoxString.导出失败+ar.Message);

                    }

                }

           }

           else

           {

               TopMessageBox.Show(TopDialogType.Warning, MessageBoxString.无数据);

           }

       }

//利用NOPI把数据写入内存,List泛型集合

public override ActionResultOutToExcel(string path, int flag)

       {

           List viewList = Grid_Center.ItemsSource asList;

           if (viewList != null && viewList.Count > 0)

           {

                List entityList= new List();

                Agent agent = new Agent();

                ActionResult ar;

                if (flag == 2) //如果导出实体,则从视图转成实体

                {

                   

                    var q=viewList[0];

                    for (int i = 0; i

                    {

                        TEntity newEntity = newTEntity();

                        q = viewList[i];

                        ar = agent.GetByView(LoginInfo.CurrentUser.员工编号, q, outnewEntity);

                        if (ar.IsSucceeded)

                        {

                           entityList.Add(newEntity);

                        }

                    }

                }

 

                List tf =agent.GetTableFiled(flag) ;

                            

                HSSFWorkbook workBook = newHSSFWorkbook();

                HSSFSheet sheet =(HSSFSheet)workBook.CreateSheet("表单");

                //创建列头

                IRow head = sheet.CreateRow(0);

                for (int lie = 0; lie

                {

                    ICell cellhead =head.CreateCell(lie);

                    cellhead.SetCellValue(tf[lie].字段名称.ToString());

                }

                int iRowIndex = 1;

                int iCellIndex = 0;

                StringBuilder Value = newStringBuilder();

                double Valueint;

                DateTime ValuedateTime;

               //导出视图列表

                if (flag == 1)

                {

                    //行

                    for (int i = 0; i

                    {

                        IRow DataRow =sheet.CreateRow(iRowIndex);

                        //列

                        for (int j = 0; j

                        {

                           //Rows[i].CreateCell(j).SetCellValue(strList[i]);

 

                            ICell cell =DataRow.CreateCell(iCellIndex);

                            if(viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null) == null)

                            {

                               Value.Append("");

                               cell.SetCellValue(Value.ToString());

                            }

                            else

                            {

                                object oo =viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null).GetType();

                               

                                if(oo.ToString()=="System.Decimal"||oo.ToString()=="System.Int16")

                                {

                                    Valueint=Convert.ToDouble( viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null));

                                    cell.SetCellValue(Valueint);

                                }

                                else if(oo.ToString() == "System.DateTime")

                                {

                                   ValuedateTime =(DateTime)viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null);

                                   

                                   cell.SetCellValue(ValuedateTime.ToShortDateString());

                                }

                                else

                                {

                                   Value.Append(viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null).ToString());

                                   cell.SetCellValue(Value.ToString());

                                }

                            }

                           

                            Value.Length = 0;

                            Valueint = 0;

                            //ValuedateTime=0

                            iCellIndex++;

                        }

                        iCellIndex = 0;

                        iRowIndex++;

                    }

                }

                //导出主表

                if (flag == 2)

                {

                    //行

                    for (int i = 0; i

                    {

                        IRow DataRow =sheet.CreateRow(iRowIndex);

                        //列

                       for (int j = 0; j< tf.Count; j++)

                        {

                           

                            ICell cell =DataRow.CreateCell(iCellIndex);

                            if(viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null) == null)

                            {

                               Value.Append("");

                               cell.SetCellValue(Value.ToString());

                            }

                            else

                            {

                                object oo =viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null).GetType();

 

                                if(oo.ToString() == "System.Decimal" || oo.ToString() == "System.Int16")

                                {

                                    Valueint =Convert.ToDouble(viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null));

                                   cell.SetCellValue(Valueint);

                               }

                                else if(oo.ToString() == "System.DateTime")

                                {

                                   ValuedateTime = (DateTime)viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null);

 

                                   cell.SetCellValue(ValuedateTime.ToShortDateString());

                                }

                                else

                                {

                                    Value.Append(viewList[i].GetType().GetProperty(tf[j].字段名称).GetValue(viewList[i],null).ToString());

                                   cell.SetCellValue(Value.ToString());

                                }

                            }

 

                            Value.Length = 0;

                            Valueint = 0;

                            //ValuedateTime=0

                            iCellIndex++;

                        }

                        iCellIndex = 0;

                        iRowIndex++;

                    }

                }

                MemoryStream ms = newMemoryStream();

                workBook.Write(ms);

                ms.Flush();

                ms.Position = 0;

                using (FileStream fs = newFileStream(path, FileMode.Create, FileAccess.Write))

                {

                    byte[] data = ms.ToArray();

                    fs.Write(data, 0,data.Length);

                    fs.Flush();

                }

                #endregion

               

              

                  

                              try

                {

                  

                    return newActionResult(true, "");

                }

                catch (Exception ex)

                {

                    return newActionResult(false, ex.Message);

                }

                #endregion

           }

           else

           {

                return new ActionResult(false,"没有可导出的内容!");

           }

       }


你可能感兴趣的:(.NET,c#,List泛型,NPOI,Excel)