using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using DevComponents.DotNetBar; using GeoScene.Globe; using GeoScene.Engine; using GeoScene.Data; using System.Collections; using System.IO; namespace Cyberpipe { public partial class FrmShResult : Office2007Form { public double dVerticalJingJuBiaoZhun = 1, dHorizontalJingJuBiaoZhun = 1; public string layername; public GSOGlobeControl globleControl1; int selectState = 0; //定位和闪烁初始化定义 int count = 0; private string flashflag = "single"; GSOFeature hlfeature = null; GSOLayer layerTemp; List<string> m_PipelineLayerNames = new List<string>();//线图层名称 GSOFeatures polygonJingJuAnalysises = new GSOFeatures(); List<string> m_vqLayer = new List<string>(); List<string> m_hqLayer = new List<string>(); MainFrm mainfrm = null; ArrayList shresultLists = new ArrayList(); /// <summary> /// /// </summary> /// <param name="_vbz"></param> /// <param name="_hbz"></param> /// <param name="_layername"></param> /// <param name="_globleControl1"></param> /// <param name="_PipelineLayerNames"></param> public FrmShResult(double _vbz, double _hbz, string _layername, GSOGlobeControl _globleControl1, List<string> _PipelineLayerNames) { dVerticalJingJuBiaoZhun = _vbz; dHorizontalJingJuBiaoZhun = _hbz; layername = _layername; globleControl1 = _globleControl1; m_PipelineLayerNames = _PipelineLayerNames; InitializeComponent(); } public FrmShResult() { // TODO: Complete member initialization } private void FrmShResult_Load(object sender, EventArgs e) { mainfrm = (MainFrm)this.Owner; layerTemp = globleControl1.Globe.Layers.Add(Application.StartupPath + "\\tempLgdData.lgd"); GSOLayer layer = globleControl1.Globe.Layers.GetLayerByCaption(layername); if (layer == null) return; GSOFeatureLayer flayer = layer as GSOFeatureLayer; GSOFeatureDataset fdataset = flayer.Dataset as GSOFeatureDataset; GSOFeatures feats = flayer.GetAllFeatures(); if (feats == null) { return; } for (int i = 0; i < feats.Length; i++) { selectState = 1; VerticalDistanceAnalysis("垂直净距分析", feats[i], m_PipelineLayerNames, dVerticalJingJuBiaoZhun, 0.0); HorizontalDistanceAnalysis("水平净距分析", feats[i], m_PipelineLayerNames, dHorizontalJingJuBiaoZhun); } HJJTxt.Text = "1"; VJJTxt.Text = "1"; //////////////////////得到问题图层列表//////////////////////// for (int i = 0; i < dgridShVResult.RowCount; i++) { m_vqLayer.Add(dgridShVResult.Rows[i].Cells["审核编号"].Value.ToString()); } for (int i = 0; i < dgridShHResult.RowCount; i++) { m_hqLayer.Add(dgridShHResult.Rows[i].Cells["审核编号1"].Value.ToString()); } ShResult sr = null; shresultLists.Clear(); for (int i = 0; i < feats.Length; i++) { GSOFeature feat = feats[i]; string featname = feat.Name; for (int j = 0; j < m_vqLayer.Count; j++) { if (m_vqLayer.Contains(featname)) { sr = new ShResult(feat.Name, "垂直净距不符合要求"); shresultLists.Add(sr); break; } else { continue; } } //shresultLists.Add(sr); } for (int i = 0; i < feats.Length; i++) { GSOFeature feat = feats[i]; string featname = feat.Name; for (int j = 0; j < m_hqLayer.Count; j++) { if (m_hqLayer.Contains(featname)) { sr = new ShResult(feat.Name, "水平净距不符合要求"); shresultLists.Add(sr); break; } else { continue; } } //shresultLists.Add(sr); } //mainfrm.shresultLists = shresultLists; } /// <summary> /// 计算指定feature对象与除了该feature所在图层之外的 所有管线图层中的所有feature对象 的垂直距离 并在表格中记录符合要求的管线 /// </summary> /// <param name="type"></param> /// <param name="caption"></param> /// <param name="feat"></param> /// <param name="dis"></param>u private void VerticalDistanceAnalysis(string type, GSOFeature selectedFeature, List<string> _pipelineLayerNames, double verticalDistance, double horizontalDistance) { if (selectedFeature == null) { selectState = 0; MessageBox.Show("请选择一条管线!"); return; } GSOGeoPolyline3D line1 = selectedFeature.Geometry as GSOGeoPolyline3D; if (line1 == null) { selectState = 0; MessageBox.Show("请选择一条管线!"); return; } GSOGeoPolygon3D polygon = line1.CreateBuffer(verticalDistance, true, 5, true, false); string caption = selectedFeature.Dataset.Caption; GSOPoint3d pntIntersect1 = new GSOPoint3d(); GSOPoint3d pntIntersect2 = new GSOPoint3d(); GSOPoint3d pntProIntersect1 = new GSOPoint3d(); GSOPoint3d pntProIntersect2 = new GSOPoint3d(); for (int i = 0; i < _pipelineLayerNames.Count; i++) { if (caption == _pipelineLayerNames[i]) { continue; } GSOLayer layer2 = globleControl1.Globe.Layers.GetLayerByCaption(_pipelineLayerNames[i]); if (layer2 != null) { GSOFeatureLayer flayer2 = layer2 as GSOFeatureLayer; GSOFeatureDataset fdataset2 = flayer2.Dataset as GSOFeatureDataset; GSOFeatures feats2 = Polygon_Contain_PointAnalysis(polygon, _pipelineLayerNames[i]); for (int j = 0; j < feats2.Length; j++) { GSOFeature feat2 = feats2[j]; GSOGeoPolyline3D line2 = feats2[j].Geometry as GSOGeoPolyline3D; if (line2 == null) continue; GSOPipeLineStyle3D pipeStyle1 = line1.Style as GSOPipeLineStyle3D; GSOPipeLineStyle3D pipeStyle2 = line2.Style as GSOPipeLineStyle3D; if (pipeStyle1 == null || pipeStyle2 == null) continue; double dDist = -1; dDist = globleControl1.Globe.Analysis3D.ComputeVerticalDistance(line1, line2, out pntIntersect1, out pntIntersect2, out pntProIntersect1, out pntProIntersect2, false); if (dDist > -1) { //dDist = Math.Abs(pntIntersect1.Z - pntIntersect2.Z) - pipeStyle1.Radius - pipeStyle2.Radius; if (dDist < verticalDistance) { int idx = -1; GSOPoint3d markerPosition = new GSOPoint3d(); if (type == "垂直净距分析") { feat2.HighLight = true; if (dDist.ToString("0.00") != "0.00") { markerPosition = LabelVerticalDistance(layerTemp, pntProIntersect1, pntProIntersect2, dDist, true); idx = dgridShVResult.Rows.Add(); dgridShVResult.Rows[idx].Cells[0].Value = caption; dgridShVResult.Rows[idx].Cells[1].Value = selectedFeature.Name; dgridShVResult.Rows[idx].Cells[2].Value = layer2.Caption; dgridShVResult.Rows[idx].Cells[3].Value = feat2.Name; dgridShVResult.Rows[idx].Cells[4].Value = dDist.ToString("0.00"); } } } } line2.ReleaseInnerPointer(); feat2.ReleaseInnerPointer(); } feats2.ReleaseInnerPointer(); } } line1.ReleaseInnerPointer(); selectedFeature.ReleaseInnerPointer(); } /// <summary> /// 查找指定图层中在 指定范围内的feature对象集合 /// </summary> /// <param name="polygon"></param> /// <param name="layername"></param> /// <returns></returns> private GSOFeatures Polygon_Contain_PointAnalysis(GSOGeoPolygon3D polygon, string layername) { GSOFeatures feats = new GSOFeatures(); GSOLayer layer = globleControl1.Globe.Layers.GetLayerByCaption(layername); if (layer == null) { return feats; } GSOFeatureLayer flayer = layer as GSOFeatureLayer; if (flayer != null) { GSOFeatureDataset fdataset = flayer.Dataset as GSOFeatureDataset; } if (polygon == null) { feats = flayer.GetAllFeatures(); } else { feats = flayer.FindFeaturesInPolygon(polygon, false); } return feats; } /// <summary> /// 在指定的两点组成的线的中间添加一个marker对象,并将marker对象添加到指定的图层中 /// </summary> /// <param name="markerLayer"></param> /// <param name="pntIntersect1"></param> /// <param name="pntIntersect2"></param> /// <param name="distance"></param> /// <returns></returns> private GSOPoint3d LabelVerticalDistance(GSOLayer markerLayer, GSOPoint3d pntIntersect1, GSOPoint3d pntIntersect2, double distance, bool markerVisible) { if (pntIntersect1 == null || pntIntersect2 == null) { return new GSOPoint3d(); } GSOGeoPolyline3D disline = new GSOGeoPolyline3D(); GSOPoint3ds point3ds = new GSOPoint3ds(); point3ds.Add(pntIntersect1); point3ds.Add(pntIntersect2); disline.AddPart(point3ds); GSOSimpleLineStyle3D style = new GSOSimpleLineStyle3D(); //创建线的风格 //设置透明度及颜色,FromArgb()中的四个参数分别为alpha、red、green、blue,取值范围为0到255 style.LineColor = Color.GreenYellow; style.LineWidth = 3; //设置线的宽度为3 style.VertexVisible = true; //显示线的节点 disline.Style = style; //把风格添加到线上 disline.AltitudeMode = EnumAltitudeMode.Absolute; GSOFeature line = new GSOFeature(); line.Geometry = disline; GSOGeoMarker dismarker = new GSOGeoMarker(); dismarker.X = pntIntersect1.X; dismarker.Y = pntIntersect1.Y; dismarker.Z = (pntIntersect1.Z + pntIntersect2.Z) / 2; dismarker.Text = "垂直"+distance.ToString("0.00") + "米"; dismarker.AltitudeMode = EnumAltitudeMode.Absolute; GSOMarkerStyle3D styleMarker = new GSOMarkerStyle3D(); GSOTextStyle styleText = new GSOTextStyle(); styleText.IsSizeFixed = true; styleText.ForeColor = Color.White; styleText.FontSize = 20; styleMarker.TextStyle = styleText; dismarker.Style = styleMarker; GSOFeature marker = new GSOFeature(); marker.Geometry = dismarker; line.Visible = marker.Visible = markerVisible; markerLayer.AddFeature(line); markerLayer.AddFeature(marker); return dismarker.Position; } private GSOPoint3d LabelHorizontalDistance(GSOLayer markerLayer, GSOPoint3d pntIntersect1, GSOPoint3d pntIntersect2, double distance, bool markerVisible) { if (pntIntersect1 == null || pntIntersect2 == null) { return new GSOPoint3d(); } GSOGeoPolyline3D disline = new GSOGeoPolyline3D(); GSOPoint3ds point3ds = new GSOPoint3ds(); point3ds.Add(pntIntersect1); point3ds.Add(pntIntersect2); disline.AddPart(point3ds); GSOSimpleLineStyle3D style = new GSOSimpleLineStyle3D(); //创建线的风格 //设置透明度及颜色,FromArgb()中的四个参数分别为alpha、red、green、blue,取值范围为0到255 style.LineColor = Color.GreenYellow; style.LineWidth = 3; //设置线的宽度为3 style.VertexVisible = true; //显示线的节点 disline.Style = style; //把风格添加到线上 disline.AltitudeMode = EnumAltitudeMode.Absolute; GSOFeature line = new GSOFeature(); line.Geometry = disline; GSOGeoMarker dismarker = new GSOGeoMarker(); dismarker.X = pntIntersect1.X; dismarker.Y = pntIntersect1.Y; dismarker.Z = (pntIntersect1.Z + pntIntersect2.Z) / 2; dismarker.Text = "水平" + distance.ToString("0.00") + "米"; dismarker.AltitudeMode = EnumAltitudeMode.Absolute; GSOMarkerStyle3D styleMarker = new GSOMarkerStyle3D(); GSOTextStyle styleText = new GSOTextStyle(); styleText.IsSizeFixed = true; styleText.ForeColor = Color.White; styleText.FontSize = 20; styleMarker.TextStyle = styleText; dismarker.Style = styleMarker; GSOFeature marker = new GSOFeature(); marker.Geometry = dismarker; line.Visible = marker.Visible = markerVisible; markerLayer.AddFeature(line); markerLayer.AddFeature(marker); return dismarker.Position; } /// <summary> /// 计算指定feature对象与除了该feature所在图层之外的 所有管线图层中的所有feature对象 的水平距离 并记录符合要求的管线 功能 /// </summary> /// <param name="type"></param> /// <param name="caption"></param> /// <param name="feat"></param> /// <param name="dis"></param> private void HorizontalDistanceAnalysis(string type, GSOFeature selectedFeature, List<string> _pipelineLayerNames, double horizontalDistance) { #region if (selectedFeature == null) { selectState = 0; MessageBox.Show("请选择一条管线!"); return; } GSOGeoPolyline3D line1 = selectedFeature.Geometry as GSOGeoPolyline3D; if (line1 == null) { selectState = 0; MessageBox.Show("请选择一条管线!"); return; } string caption = selectedFeature.Dataset.Caption; GSOPoint3d pntIntersect1 = new GSOPoint3d(); GSOPoint3d pntIntersect2 = new GSOPoint3d(); GSOPoint3d pntProIntersect1 = new GSOPoint3d(); GSOPoint3d pntProIntersect2 = new GSOPoint3d(); GSOGeoPolygon3D polygon = line1.CreateBuffer(horizontalDistance, true, 5, true, false); GSOFeature new_feat = new GSOFeature(); new_feat.Geometry = polygon; polygonJingJuAnalysises.Add(new_feat); layerTemp.AddFeature(new_feat); for (int i = 0; i < _pipelineLayerNames.Count; i++) { if (caption == _pipelineLayerNames[i]) { continue; } GSOLayer layer2 = globleControl1.Globe.Layers.GetLayerByCaption(_pipelineLayerNames[i]); if (layer2 != null) { GSOFeatureLayer flayer2 = layer2 as GSOFeatureLayer; GSOFeatureDataset fdataset2 = flayer2.Dataset as GSOFeatureDataset; GSOFeatures feats2 = Polygon_Contain_PointAnalysis(polygon, _pipelineLayerNames[i]); for (int j = 0; j < feats2.Length; j++) { GSOFeature feat2 = feats2[j]; GSOGeoPolyline3D line2 = feats2[j].Geometry as GSOGeoPolyline3D; if (line2 == null) { continue; } GSOPipeLineStyle3D pipeStyle1 = line1.Style as GSOPipeLineStyle3D; GSOPipeLineStyle3D pipeStyle2 = line2.Style as GSOPipeLineStyle3D; if (pipeStyle1 == null || pipeStyle2 == null) { continue; } double dDist = -1; dDist = globleControl1.Globe.Analysis3D.ComputeHorizonDistance(line1, line2, out pntIntersect1, out pntIntersect2, out pntProIntersect1, out pntProIntersect2, false); if (dDist > -1) { //dDist = Math.Abs(pntIntersect1.Z - pntIntersect2.Z) - pipeStyle1.Radius - pipeStyle2.Radius; if (dDist < horizontalDistance) { GSOPoint3d markerPosition = new GSOPoint3d(); if (type == "水平净距分析") { if (dDist.ToString("0.00") != "0.00") { feat2.HighLight = true; int idx = dgridShHResult.Rows.Add(); markerPosition = LabelHorizontalDistance(layerTemp, pntProIntersect1, pntProIntersect2, dDist, true); dgridShHResult.Rows[idx].Tag = feat2; dgridShHResult.Rows[idx].Cells[0].Value = caption; dgridShHResult.Rows[idx].Cells[1].Value = selectedFeature.Name; dgridShHResult.Rows[idx].Cells[2].Value = layer2.Caption; dgridShHResult.Rows[idx].Cells[3].Value = feats2[j].Name; dgridShHResult.Rows[idx].Cells[4].Value = dDist.ToString("0.00"); } } } } } } } #endregion } /// <summary> /// 双击后围绕问题对象旋转 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgridShVResult_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { string shlayer =(dgridShVResult.Rows[e.RowIndex].Cells["审核图层"].Value.ToString()); string shlayerid = (dgridShVResult.Rows[e.RowIndex].Cells["审核编号"].Value.ToString()); GSOLayer centerlayer = globleControl1.Globe.Layers.GetLayerByCaption(shlayer); GSOFeatures feats = centerlayer.GetFeatureByName(shlayerid, true); if (centerlayer != null) { for (int j = 0; j < feats.Length; j++) { if (feats[j].Name == shlayerid) { //m_feature = features[j]; GSOFeature rowFeature = feats[j]; if (rowFeature == null) continue; if (rowFeature.Geometry != null && rowFeature.Geometry.Type == EnumGeometryType.GeoPolyline3D) { GSOGeoPolyline3D line = rowFeature.Geometry as GSOGeoPolyline3D; double length = line.GetSpaceLength(true, 6378137);//线的长度; GSOGeoPolyline3D lineLine = line.GetSegment(0, length / 2); GSOPoint3d point3d = lineLine[lineLine.PartCount - 1][lineLine[lineLine.PartCount - 1].Count - 1]; globleControl1.Globe.JumpToPosition(point3d, EnumAltitudeMode.Absolute, 50); globleControl1.Globe.FlyAroundPosition(point3d, true, 10, EnumFlyRepeatValueType.Degrees); ///高亮 //LightMenu_Click(sender, e); hlfeature = rowFeature; highlight(); } else { globleControl1.Globe.JumpToFeature(rowFeature, 300); } } } } } /// <summary> /// 双击后围绕问题对象旋转 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgridShHResult_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { string shlayer = (dgridShHResult.Rows[e.RowIndex].Cells["审核图层1"].Value.ToString()); string shlayerid = (dgridShHResult.Rows[e.RowIndex].Cells["审核编号1"].Value.ToString()); GSOLayer centerlayer = globleControl1.Globe.Layers.GetLayerByCaption(shlayer); GSOFeatures feats = centerlayer.GetFeatureByName(shlayerid, true); if (centerlayer != null) { for (int j = 0; j < feats.Length; j++) { if (feats[j].Name == shlayerid) { //m_feature = features[j]; GSOFeature rowFeature = feats[j]; if (rowFeature == null) continue; if (rowFeature.Geometry != null && rowFeature.Geometry.Type == EnumGeometryType.GeoPolyline3D) { GSOGeoPolyline3D line = rowFeature.Geometry as GSOGeoPolyline3D; double length = line.GetSpaceLength(true, 6378137);//线的长度; GSOGeoPolyline3D lineLine = line.GetSegment(0, length / 2); GSOPoint3d point3d = lineLine[lineLine.PartCount - 1][lineLine[lineLine.PartCount - 1].Count - 1]; globleControl1.Globe.JumpToPosition(point3d, EnumAltitudeMode.Absolute, 30); globleControl1.Globe.FlyAroundPosition(point3d, true, 10, EnumFlyRepeatValueType.Degrees); //高亮显示 hlfeature = rowFeature; highlight(); } else { globleControl1.Globe.JumpToFeature(rowFeature, 300); } } } } } /// <summary> /// 清除渲染结果 /// </summary> public void ClearAnalyseResult(){ // dgridShHResult.Rows.Clear(); dgridShVResult.Rows.Clear(); //globleControl1.Globe.MemoryLayer.RemoveAllFeature(); //globleControl1.Globe.ClearAnalysis(); layerTemp.RemoveAllFeature(); globleControl1.Refresh(); } /// <summary> /// 高亮显示 /// </summary> public void highlight(){ flashflag = "single"; timer1.Start(); } /// <summary> /// 使用timer实现管线的闪烁效果 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_Tick(object sender, EventArgs e) { GSOFeature rowFeature = hlfeature;//as GSOFeature; if (rowFeature == null) return; if (count < 40) { count++; if (flashflag == "single") { if (rowFeature != null) { if (count % 2 != 0) { rowFeature.HighLight = true; globleControl1.Refresh(); } else { rowFeature.HighLight = false; globleControl1.Refresh(); } } } } else { timer1.Stop(); rowFeature.HighLight = false; count = 0; } } /// <summary> /// 修改净距标准后重新计算 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { if ((HJJTxt.Text != "" && VJJTxt.Text != "" && dgridShVResult.RowCount != 0) || (HJJTxt.Text != "" && VJJTxt.Text != "" && dgridShHResult.RowCount != 0)) { ClearAnalyseResult(); layerTemp = globleControl1.Globe.Layers.Add(Application.StartupPath + "\\tempLgdData.lgd"); GSOLayer layer = globleControl1.Globe.Layers.GetLayerByCaption(layername); if (layer == null) return; GSOFeatureLayer flayer = layer as GSOFeatureLayer; GSOFeatureDataset fdataset = flayer.Dataset as GSOFeatureDataset; GSOFeatures feats = flayer.GetAllFeatures(); if (feats == null) { return; } List<ShResult> shresultList = new List<ShResult>(); for (int i = 0; i < feats.Length; i++) { selectState = 1; VerticalDistanceAnalysis("垂直净距分析", feats[i], m_PipelineLayerNames, double.Parse(VJJTxt.Text.Trim().ToString()), 0.0); HorizontalDistanceAnalysis("水平净距分析", feats[i], m_PipelineLayerNames, double.Parse(HJJTxt.Text.Trim().ToString())); } //////////////////////得到问题图层列表//////////////////////// shresultLists.Clear(); for (int i = 0; i < dgridShVResult.RowCount; i++) { m_vqLayer.Add(dgridShVResult.Rows[i].Cells["审核编号"].Value.ToString()); } for (int i = 0; i < dgridShHResult.RowCount; i++) { m_hqLayer.Add(dgridShHResult.Rows[i].Cells["审核编号1"].Value.ToString()); } ShResult sr = null; for (int i = 0; i < feats.Length; i++) { GSOFeature feat = feats[i]; string featname = feat.Name; for (int j = 0; j < m_vqLayer.Count; j++) { if (m_vqLayer.Contains(featname)) { sr = new ShResult(feat.Name, "垂直净距不符合要求"); shresultLists.Add(sr); break; } else { continue; } } //shresultLists.Add(sr); } for (int i = 0; i < feats.Length; i++) { GSOFeature feat = feats[i]; string featname = feat.Name; for (int j = 0; j < m_hqLayer.Count; j++) { if (m_hqLayer.Contains(featname)) { sr = new ShResult(feat.Name, "水平净距不符合要求"); shresultLists.Add(sr); break; } else { continue; } } //shresultLists.Add(sr); } // mainfrm.shresultLists = shresultLists; } else { MessageBox.Show("请输入净距标准!"); return; } } private void FrmShResult_FormClosed(object sender, FormClosedEventArgs e) { mainfrm.boolfrmShResult = false; this.Close(); } /// <summary> /// 垂直净距导出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonEXP_Click(object sender, EventArgs e) { if (dgridShVResult.Rows.Count > 0) { string stype = "垂直净距"; ExportExcel(stype, dgridShVResult, null); } else { MessageBox.Show("表格内容为空!", "提示"); } } /// <summary> /// 水平净距导出 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void buttonExp1_Click(object sender, EventArgs e) { if (dgridShHResult.Rows.Count > 0) { string stype = "水平净距"; ExportExcel(stype, dgridShHResult, null); } else { MessageBox.Show("表格内容为空!", "提示"); } } /// <summary> /// 导出指定DataGridView控件中的内容 /// </summary> /// <param name="type"></param> /// <param name="_dataGridView"></param> private void ExportExcel(string type, DataGridView _dataGridView, ListBox _listBox) { if (_dataGridView.Rows.Count > 0) { SaveFileDialog dlg = new SaveFileDialog(); dlg.Filter = "Excel files (*.xls)|*.xls"; dlg.FilterIndex = 0; dlg.RestoreDirectory = true; dlg.Title = "保存为Excel文件"; dlg.FileName = type + "-" + DateTime.Now.ToString("yyyyMMdd") + ".xls"; if (dlg.ShowDialog() == DialogResult.OK) { Stream myStream; myStream = dlg.OpenFile(); StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding(-0)); string columnTitle = ""; try { if (_listBox != null) { string strList = ""; for (int i = 0; i < _listBox.Items.Count; i++) { strList += _listBox.Items[i].ToString() + @"/"; } sw.WriteLine("内容:" + type + " 日期:" + DateTime.Now.ToString("yyyy-MM-dd") + " 结果:" + strList); } else { sw.WriteLine("内容:" + type + " 日期:" + DateTime.Now.ToString("yyyy-MM-dd")); } //写入列标题 for (int i = 0; i < _dataGridView.ColumnCount; i++) { if (i > 0) { columnTitle += "\t"; } columnTitle += _dataGridView.Columns[i].HeaderText; } sw.WriteLine(columnTitle); //写入列内容 for (int j = 0; j < _dataGridView.Rows.Count; j++) { string columnValue = ""; for (int k = 0; k < _dataGridView.Columns.Count; k++) { if (k > 0) { columnValue += "\t"; } if (_dataGridView.Rows[j].Cells[k].Value == null) { columnValue += ""; } else { columnValue += _dataGridView.Rows[j].Cells[k].Value.ToString().Trim(); } } sw.WriteLine(columnValue); } sw.Close(); myStream.Close(); if (MessageBox.Show("导出Excel文件成功!是否打开?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes) { System.Diagnostics.Process.Start(dlg.FileName); } } catch (Exception ex) { //MessageBox.Show(ex.ToString()); } finally { sw.Close(); myStream.Close(); } } } } } }