zoukankan      html  css  js  c++  java
  • C#从入门到放弃--WPF导入导出

    一、导出

    1                  <Button
    2                             x:Name="btnExport"
    3                             IsEnabled="{Binding IsEnabled}"
    4                             Margin="{DynamicResource BtnMargin}"
    5                             controls:ButtonHelper.IconContent="{DynamicResource Icon_Export}"
    6                             Content="{DynamicResource Export}" Click="Export_ImageButton_Click" Height="36" VerticalAlignment="Bottom" />
    1、IndexPage.xmal
     #region 事件
            //导出
            public void Export_ImageButton_Click(object sender, RoutedEventArgs e)
            {
    
                try
                {
    
                    IWorkbook workbook = new HSSFWorkbook();
                    string filename = "条质量查询报表";
                    ISheet sheet = workbook.CreateSheet(filename);
    
    
                    #region 设置文件样式
                    ICellStyle style = workbook.CreateCellStyle();
                    //字体
                    IFont font = workbook.CreateFont();
                    font.FontHeightInPoints = 10;//字体
                    font.FontName = "微软雅黑";
                    font.IsBold = true;
    
                    //背景色
                    style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                    style.FillPattern = FillPattern.SolidForeground;
                    style.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                    style.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
                    style.SetFont(font);
                    style.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                    style.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                    style.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                    style.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
    
    
    
                    #endregion
    
                    #region 获取查询条件下所有数据源
                    ObservableCollection<StripQualityModel> models = new ObservableCollection<StripQualityModel>();
                    var FeedbackData = this.Framework.GetData(Services.GetStripQualityInfo, this.vm.AMOEBA, this.vm.LINE_CODE, this.vm.PROC_START_TIME.ToString(), this.vm.P                    ROC_END_TIME.ToString(),
                     this.vm.WORKGROUP_CODE, this.vm.QUALITY_CATEGORY_CODE, 0, 1000000); /// add params 
    
                    if (!FeedbackData.success)
                    {
                        MessageBox.Show(FeedbackData.message.content);
                        return;
                    }
    
                    if (FeedbackData.data != null)
                    {
                        models = JsonConvert.DeserializeObject<ObservableCollection<StripQualityModel>>(Convert.ToString(FeedbackData.data.data));
    
                    }
    
                    #endregion
    
                    #region  表格数据
                    List<String> listTitle = new List<String>();
                    listTitle.Add("序号");
                    listTitle.Add("排产日期");
                    listTitle.Add("巴号");
                    listTitle.Add("产线编码");
                    listTitle.Add("班组编码");
                    listTitle.Add("班组名称");
                    listTitle.Add("班次");
                    listTitle.Add("品质分类");
                    listTitle.Add("最大值");
                    listTitle.Add("最小值");
                    listTitle.Add("平均值");
                    listTitle.Add("检验次数");
                    listTitle.Add("标准值");
                    listTitle.Add("标准最大值");
                    listTitle.Add("标准最小值");
    
                    int iCurrentRowIndex =0;
                    IRow rowTitle = sheet.CreateRow(iCurrentRowIndex);
    
                    for (int i = 0; i < listTitle.Count; i++)
                    {
    
                        ICell cell = rowTitle.CreateCell(i);
                        cell.CellStyle = style;
                        cell.SetCellValue(listTitle[i]);
                        sheet.SetColumnWidth(i, (sheet.GetColumnWidth(i) + Convert.ToInt32(sheet.GetColumnWidth(i) * 0.5)));//设置单元格宽度
                    }
    
                    iCurrentRowIndex++;
                    if (models !=null)
                    {
                        foreach (var item in models)
                        {
                            IRow currentRow = sheet.CreateRow(iCurrentRowIndex);
                            currentRow.CreateCell(0).SetCellValue((iCurrentRowIndex).ToString());
                            currentRow.CreateCell(1).SetCellValue(item.SHIFT_DATE);
                            currentRow.CreateCell(2).SetCellValue(item.AMOEBA);
                            currentRow.CreateCell(3).SetCellValue(item.WORKCENTER_CODE);
                            currentRow.CreateCell(4).SetCellValue(item.WORKGROUP_CODE);
                            currentRow.CreateCell(5).SetCellValue(item.WORKGROUP_NAME);
                            currentRow.CreateCell(6).SetCellValue(item.SHIFT_CODE);
                            currentRow.CreateCell(7).SetCellValue(item.QUALITY_CATEGORY_CODE);
                            currentRow.CreateCell(8).SetCellValue(item.MAX_VALUE.ToString());
                            currentRow.CreateCell(9).SetCellValue(item.MIN_VALUE.ToString());
                            currentRow.CreateCell(10).SetCellValue(item.AVRGE_VALUE.ToString());
                            currentRow.CreateCell(11).SetCellValue(item.INSPECTION_TIME.ToString());
                            currentRow.CreateCell(12).SetCellValue(item.CARDINAL_NUMBER.ToString());
                            currentRow.CreateCell(13).SetCellValue(item.MAX_DEVIATION.ToString());
                            currentRow.CreateCell(14).SetCellValue(item.MIN_DEVIATION.ToString());
                            iCurrentRowIndex++;
                        }
                    }
                    #endregion
    
    
                    System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
    
                    sfd.FileName = filename + DateTime.Now.ToString("yyyyMMddhhmmss");
                    sfd.Filter = "Excel |*.xls";
                    //sfd.DefaultExt = "Excel (*.XLSX)";
                    sfd.Title = filename;
    
                    sfd.AddExtension = false;
    
                    var result = sfd.ShowDialog();
    
                    if (result != System.Windows.Forms.DialogResult.Cancel)
                    {
                        //新建文件流  
                        using (FileStream stream = new FileStream(sfd.FileName, FileMode.OpenOrCreate, FileAccess.Write))
                        {
                            int i = workbook.NumberOfSheets;
                            workbook.Write(stream);
                            stream.Flush();
    
                        }
    
                    }
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show(ex.Message);
                }
    
            }
    
            #endregion
    IndexpPage.xaml.cs

    二、导入

    IndexPage.xaml
     IndexPage.xaml.cs
  • 相关阅读:
    用任务计划管理计划任务对付任务计划-禁止WPS提示升级
    破解激活Win10无风险?激活后删除激活工具无影响===http://www.pconline.com.cn/win10/693/6932077_all.html#content_page_4
    VS2013 密钥 – 所有版本
    2017面试题1
    模拟锚点
    输入框被软键盘遮
    资源(GitHub)
    全国城市部分js
    subline3 插件
    app下载——js设备判断
  • 原文地址:https://www.cnblogs.com/xuhp/p/9795514.html
Copyright © 2011-2022 走看看