问题描述
创建一个窗口程序,3个button:1、打开-OpenFileDialog实现自选路径打开一个XML文件。2、读取-将打开的这个XML文档的内容读取到一个控件里(临时)比如dataset中,其他的控件也可以。3、写入-从dataset中提取一部分我需要的XML中的关键字写入到EXCEL中(重新生成一个,或者是指定的EXCEL都可以)。小弟才疏学浅,请高人指点,请您附加代码,不胜感激!
解决方案
解决方案二:
protectedvoidExportExcel(){gridbind();if(ds1==null)return;stringsaveFileName="";//boolfileSaved=false;SaveFileDialogsaveDialog=newSaveFileDialog();saveDialog.DefaultExt="xls";saveDialog.Filter="Excel文件|*.xls";saveDialog.FileName="Sheet1";saveDialog.ShowDialog();saveFileName=saveDialog.FileName;if(saveFileName.IndexOf(":")<0)return;//被点了取消//excelapp.Workbooks.Open(App.path&工程进度表.xls)Excel.ApplicationxlApp=newExcel.Application();objectmissing=System.Reflection.Missing.Value;if(xlApp==null){MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");return;}Excel.Workbooksworkbooks=xlApp.Workbooks;Excel.Workbookworkbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);Excel.Worksheetworksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1Excel.Rangerange;stringoldCaption=Title_label.Text.Trim();longtotalCount=ds1.Tables[0].Rows.Count;longrowRead=0;floatpercent=0;worksheet.Cells[1,1]=Title_label.Text.Trim();//写入字段for(inti=0;i<ds1.Tables[0].Columns.Count;i++){worksheet.Cells[2,i+1]=ds1.Tables[0].Columns.ColumnName;range=(Excel.Range)worksheet.Cells[2,i+1];range.Interior.ColorIndex=15;range.Font.Bold=true;}//写入数值Caption.Visible=true;for(intr=0;r<ds1.Tables[0].Rows.Count;r++){for(inti=0;i<ds1.Tables[0].Columns.Count;i++){worksheet.Cells[r+3,i+1]=ds1.Tables[0].Rows[r];}rowRead++;percent=((float)(100*rowRead))/totalCount;this.Caption.Text="正在导出数据["+percent.ToString("0.00")+"%]...";Application.DoEvents();}worksheet.SaveAs(saveFileName,missing,missing,missing,missing,missing,missing,missing,missing);this.Caption.Visible=false;this.Caption.Text=oldCaption;range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[ds1.Tables[0].Rows.Count+2,ds1.Tables[0].Columns.Count]);range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex=Excel.XlColorIndex.xlColorIndexAutomatic;range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle=Excel.XlLineStyle.xlContinuous;range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight=Excel.XlBorderWeight.xlThin;if(ds1.Tables[0].Columns.Count>1){range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex=Excel.XlColorIndex.xlColorIndexAutomatic;}workbook.Close(missing,missing,missing);xlApp.Quit();}