示例#1
0
        //导出数据
        public static bool ExportDataGridview(DataGridViewX gridView, List <string> lstFields, string defaultName)
        {
            //添加进度条 ygc 2012-10-8
            SysCommon.CProgress vProgress = new SysCommon.CProgress();
            vProgress.ShowDescription    = true;
            vProgress.ShowProgressNumber = true;
            vProgress.TopMost            = true;
            vProgress.EnableCancel       = true;
            vProgress.EnableUserCancel(true);
            vProgress.ProgresssValue = 0;
            vProgress.Step           = 1;
            vProgress.ShowProgress();
            vProgress.SetProgress("正在导出" + defaultName + "数据......");
            Microsoft.Office.Interop.Excel.Application excel = null;
            Workbook wb = null;

            try
            {
                if (gridView.Rows.Count == 0)
                {
                    return(false);
                }
                //建立Excel对象
                excel         = new Microsoft.Office.Interop.Excel.Application();
                wb            = excel.Application.Workbooks.Add(true);
                excel.Visible = true;
                wb.Application.ActiveWindow.Caption = defaultName;

                //生成字段名称
                //for (int i = 0; i < gridView.ColumnCount; i++)
                //{
                //    if(!lstFields.Contains(gridView.Columns[i].HeaderText)) continue;
                //    excel.Cells[1, i + 1] = gridView.Columns[i].HeaderText;
                //}

                for (int i = 0; i < lstFields.Count; i++)
                {
                    //if (!lstFields.Contains(gridView.Columns[i].HeaderText)) continue;
                    excel.Cells[1, i + 1] = gridView.Columns[lstFields[i]].HeaderText;
                }

                vProgress.MaxValue = gridView.Columns.Count * gridView.Rows.Count;
                int t = 0;
                //填充数据
                for (int i = 0; i < gridView.RowCount; i++)
                {
                    for (int j = 0; j < lstFields.Count; j++)
                    {
                        //if (!lstFields.Contains(gridView.Columns[j].HeaderText)) continue;
                        int intFieldIndex = gridView.Columns.IndexOf(gridView.Columns[lstFields[j]]);

                        if (gridView[intFieldIndex, i].ValueType == typeof(string))
                        {
                            if (gridView[intFieldIndex, i].Value != null)
                            {
                                excel.Cells[i + 2, j + 1] = "'" + gridView[intFieldIndex, i].Value.ToString();
                            }
                        }
                        else
                        {
                            if (gridView[intFieldIndex, i].Value != null)
                            {
                                excel.Cells[i + 2, j + 1] = gridView[intFieldIndex, i].Value.ToString();
                            }
                        }
                        t++;
                        vProgress.ProgresssValue = t;
                    }
                }
                vProgress.Close();
                Microsoft.Office.Core.FileDialog fd = wb.Application.get_FileDialog(Microsoft.Office.Core.MsoFileDialogType.msoFileDialogSaveAs);
                fd.InitialFileName = defaultName;
                int result = fd.Show();
                if (result == 0)
                {
                    return(true);
                }
                string fileName = fd.InitialFileName;
                if (!string.IsNullOrEmpty(fileName))
                {
                    if (fileName.IndexOf(".xls") == -1)
                    {
                        fileName += ".xls";
                    }
                    wb.SaveAs(fileName, XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                }
                return(true);
            }
            catch
            {
                vProgress.Close();
                return(false);
            }
        }
示例#2
0
        private void dgvCheckItem_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvCheckItem.CurrentRow == null)
            {
                return;
            }
            if (dgvCheckItem.CurrentRow.Cells["IsRight"].Value.ToString() != "检查未通过")
            {
                return;
            }
            string        LayerName    = dgvCheckItem.CurrentRow.Cells["LayerName"].Value.ToString();
            IFeatureClass pFeaureClass = GetFeatureClassByName(m_DicErrorData, LayerName);

            if (pFeaureClass == null)
            {
                return;
            }
            string Condition = dgvCheckItem.CurrentRow.Cells["CONDITION"].Value.ToString();

            System.Data.DataTable ErrorTable = InitializeErrorTable("错误信息");
            IQueryFilter          pFilter    = new QueryFilterClass();

            pFilter.WhereClause = Condition;
            IFeatureCursor pFeatureCursor = null;

            SysCommon.CProgress vProgress = new SysCommon.CProgress();
            vProgress.ShowDescription    = true;
            vProgress.ShowProgressNumber = true;
            vProgress.TopMost            = true;
            vProgress.EnableCancel       = false;
            vProgress.EnableUserCancel(false);
            vProgress.MaxValue       = pFeaureClass.FeatureCount(pFilter);
            vProgress.ProgresssValue = 0;
            vProgress.Step           = 1;
            vProgress.ShowProgress();
            try
            {
                pFeatureCursor = pFeaureClass.Search(pFilter, false);
                IFeature pFeature = pFeatureCursor.NextFeature();
                while (pFeature != null)
                {
                    int     shengIndex = pFeature.Fields.FindField("sheng");
                    DataRow newRow     = ErrorTable.NewRow();
                    newRow["LayerName"] = pFeaureClass.AliasName;
                    newRow["sheng"]     = ModXZQ.GetXzqName(Plugin.ModuleCommon.TmpWorkSpace, pFeature.get_Value(shengIndex).ToString().Substring(0, 2));
                    int shiIndex = pFeature.Fields.FindField("shi");
                    newRow["shi"] = ModXZQ.GetXzqName(Plugin.ModuleCommon.TmpWorkSpace, pFeature.get_Value(shiIndex).ToString().Substring(0, 4));
                    int xianIndex = pFeature.Fields.FindField("xian");
                    newRow["xian"] = ModXZQ.GetXzqName(Plugin.ModuleCommon.TmpWorkSpace, pFeature.get_Value(xianIndex).ToString().Substring(0, 6));
                    int xiangIndex = pFeature.Fields.FindField("xiang");
                    newRow["xiang"] = ModXZQ.GetXzqName(Plugin.ModuleCommon.TmpWorkSpace, pFeature.get_Value(xiangIndex).ToString().Substring(0, 8));
                    int cunIndex = pFeature.Fields.FindField("cun");
                    newRow["cun"] = ModXZQ.GetXzqName(Plugin.ModuleCommon.TmpWorkSpace, pFeature.get_Value(cunIndex).ToString().Substring(0, 10));
                    int xbhIndex = pFeature.Fields.FindField("xbh");
                    newRow["xbh"] = pFeature.get_Value(xbhIndex).ToString();
                    ErrorTable.Rows.Add(newRow);
                    pFeature = pFeatureCursor.NextFeature();
                    vProgress.ProgresssValue += 1;
                }
            }
            catch (Exception ex)
            {
                vProgress.Close();
            }
            vProgress.Close();
            dgvCheckResultData.DataSource = ErrorTable;
            DataGridViewCellStyle ds = new DataGridViewCellStyle();

            ds.ForeColor = Color.Red;
            for (int i = 0; i < dgvCheckResultData.Rows.Count; i++)
            {
                dgvCheckResultData.Rows[i].DefaultCellStyle = ds;
            }
            for (int j = 0; j < dgvCheckResultData.Columns.Count; j++)
            {
                dgvCheckResultData.Columns[j].HeaderText   = ErrorTable.Columns[j].Caption;
                dgvCheckResultData.Columns[j].SortMode     = DataGridViewColumnSortMode.NotSortable;
                dgvCheckResultData.Columns[j].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
        }