我发现把HTML文件另存为.xls文件之后,用EXCEL打开,然后在EXCEL里做任意修改保存后,它就不是之前的HTML了,源文件立刻变成EXCEL的了。所以我就想,能不能模拟一个用EXCEL打开一个HTML文件,然后修改、保存的功能?

解决方案 »

  1.   

    读取HTML内容,导出建立Excel文件,
      

  2.   

    那你为什么非要用Excel来编码HTML呢?
      

  3.   


    protected void ExportExcel()
      {
       gridbind(); 
       if(ds1==null) return; 
      
       string saveFileName="";
    //   bool fileSaved=false;
       SaveFileDialog saveDialog=new SaveFileDialog();
       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.Application xlApp=new Excel.Application();
       object missing=System.Reflection.Missing.Value; 
         if(xlApp==null)
       {
        MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
        return;
       }
       Excel.Workbooks workbooks=xlApp.Workbooks;
       Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
       Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
       Excel.Range range;
        
      
       string oldCaption=Title_label .Text.Trim ();
       long totalCount=ds1.Tables[0].Rows.Count;
       long rowRead=0;
       float percent=0; 
      
       worksheet.Cells[1,1]=Title_label .Text.Trim ();
       //写入字段
       for(int i=0;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(int r=0;r    {
        for(int i=0;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();
      } 
      

  4.   


    你说的是自己读写EXCEL,我是说模拟打开HTML--修改(随便改一下就行)--保存这样的一个操作
      

  5.   


    你说的这个貌似是DataTable导出到EXCEL,和我的完全不同,我的是纯HTML,就是table tr td,明白?
      

  6.   

    如果你是要把HTML里面的表格保存成EXECL文件的话可以把这段HTML作为XML对象来操作,然后一个节点一个节点的读,在一个CELL一个CELL得写进表格里