• 表格导入和导出


            
                DataTable DTDaoRu = new DataTable();
                OpenFileDialog opf = new OpenFileDialog();
                opf.Filter = "电子表格|*.xls;*.xlsx";
                if (opf.ShowDialog() == DialogResult.OK)
                {
                    Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(opf.FileName);
                    Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
                    Aspose.Cells.ExportTableOptions p = new Aspose.Cells.ExportTableOptions();
                    p.ExportAsString = true;//内容转化为字符串
                    p.ExportColumnName = true;//首行转换列名
                    DataTable dt = cells.ExportDataTable(0, 0, cells.MaxDataRow + 1, cells.MaxDataColumn + 1, p);
                }     


    循环导出

    for (int i = 2; i < cells.MaxDataRow + 1; i++)//前两行是标题和列名
    {
    List<object> clist = new List<object>();
    for (int j = 0; j < cells.MaxDataColumn + 1; j++)
    {
    string s = cells[i, j].StringValue.Trim();
    clist.Add(s);
    }
    GridRow gr = new GridRow(clist);
    spgData.SPG.PrimaryGrid.Rows.Add(gr);
    }

    
    

     导出

                It_helped ISs = new It_helped();
                if (mydgv.Rows.Count > 0)
                {
                    List<int> Basetype = new List<int>();
    
                    DataTable DTS = new DataTable();
                    for (int a = 0; a < mydgv.Columns.Count; a++)
                    {
                        if (mydgv.Columns[a].Visible == false)
                        {
                            Basetype.Add(a);
                        }
                        DTS.Columns.Add(mydgv.Columns[a].HeaderText);
                    }
                    for (int a = 0; a < mydgv.Rows.Count; a++)
                    {
                        if ((mydgv.Rows[a].Cells["Base_Code"].Value + "").Trim() != "")
                        {
                            DataRow DR = DTS.NewRow();
                            for (int b = 0; b < mydgv.Columns.Count; b++)
                            {
                                DR[b] = (mydgv.Rows[a].Cells[b].Value + "").Trim().ToString();
                            }
                            DTS.Rows.Add(DR);
                        }
                    }
                    for (int cc = 0; cc < Basetype.Count; cc++)
                    {
                        DTS.Rows.RemoveAt(int.Parse(Basetype[cc].ToString()));
                    }
                    string fileDefaultName = "导出Excel";
                    string path;
                    SaveFileDialog savefile = new SaveFileDialog();
                    savefile.FileName = fileDefaultName;
                    savefile.Filter = "EXECL文件(*.xlsx) |*.xlsx |EXECL2003文件(*.xls) |*.xls |所有文件(*.*) |*.*";
                    if (savefile.ShowDialog() == DialogResult.OK)
                    {
                        try
                        {
                            ISs.importExcel(DTS, US_GV.UserDeptName + "出库卫材详情", savefile.FileName);
                            MessageBox.Show("导出成功");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("导出失败:" + ex.Message);
                        }
                    }
    
    
    
    
                }
                else
                {
                    MessageBox.Show("请检查表格是否有数据!");
                    return;
                }
  • 相关阅读:
    WPF FAQ (from Syncfusion)
    父窗口与子窗口的层次关系
    [WPF疑难]如何禁用WPF窗口的系统菜单(SystemMenu)
    【讨论】不得不重视的问题:信息太多!信息太杂!
    javaScript系列 [08]javaScript和JSON (进阶)
    javaScript系列 [11]Canvas绘图(路径)
    javaScript系列 [10]Canvas绘图(基础)
    Hexo + Github搭建个人博客
    javaScript系列 [07]日期类型和计时器
    javaScript系列 [09]javaScript和JSON (拓展)
  • 原文地址:https://www.cnblogs.com/skyhorseyk/p/10156656.html
Copyright © 2020-2023  润新知