给单元格赋值:
V.Cells[1,4].Value := '第一行第四列';

解决方案 »

  1.   

    首先 MyWorkSheet := v.ActiveWorkBook.ActiveSheet
    在Excel中录制宏,然后编辑宏代码,将其代码拷贝到delphi中。前面加上
    myWorkSheet.
      

  2.   

    有没有关于这个得代码实例,我想知道
    打开一个Excel文件后,怎样在进行对其中的单元格进行赋值
      

  3.   

    try
        e:=CreateOleObject('Excel.Application');
        e.workbooks.add();
        e.visible:=true;
      except
        showmessage('ÄúµÄ¼ÆËã»úÖÐδ°²×°MicroSoft Excel');
        exit;
      end;
      e.ActiveWindow.DisplayGridlines:=false;
    //µÃµ½ÁÐÊý
      colcount:=DBGrid.Columns.Count ;
      setlength(columnwidth,colcount);
      for i:=low(columnwidth) to high(columnwidth) do
        columnwidth[i]:=0;//Ìîд±êÌâ
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].HorizontalAlignment := xlGeneral;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].VerticalAlignment := xlBottom;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].WrapText := False;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Orientation := 0;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].AddIndent := False;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].ShrinkToFit := False;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].MergeCells := True;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].HorizontalAlignment := xlCenter;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].VerticalAlignment := xlBottom;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Font.Name := 'ºÚÌå';
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Font.Size := 16;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Font.Strikethrough := False;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Font.Superscript := False;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Font.Subscript := False;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Font.OutlineFont := False;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Font.Shadow := False;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Font.Underline := xlUnderlineStyleNone;
        e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[2,1],e.Workbooks[1].Sheets[1].Cells[2,ColCount]].Font.ColorIndex := xlAutomatic;
      e.Workbooks[1].Sheets[1].Cells[2,1] := Title;
    //Ìîд±¨±íµ¥Î»,ʱ¼ä
      e.Workbooks[1].Sheets[1].Cells[4,1]:='±¨±íµ¥Î»:';
      e.Workbooks[1].Sheets[1].Cells[4,4] :='±¨±íʱ¼ä:'+ DateTime;
    //Ìîд±íÍ·
      e.Workbooks[1].Sheets[1].Cells[5,1] :=DBGrid.Fields[0].DisplayName;
      columnwidth[0]:=length(DBGrid.Fields[0].DisplayName);
      for i:=2 to ColCount do
      begin
        e.Workbooks[1].Sheets[1].Cells[5,i] :=DBGrid.Fields[i-1].DisplayName;
        columnwidth[i-1]:=length(DBGrid.Fields[i-1].DisplayName);
      end;
    //ÌîдÊý¾Ý
      i:=1;
      DBGrid.DataSource.DataSet.First ;
      while not DBGrid.DataSource.DataSet.Eof do
      begin
        for j:=1 to ColCount do
        begin
          e.Workbooks[1].Sheets[1].Cells[5+i,j] := DBGrid.Fields[j-1].AsString;
          if columnwidth[j-1]<length(DBGrid.Fields[j-1].AsString) then
            columnwidth[j-1]:=length(DBGrid.Fields[j-1].AsString);
        end;
        DBGrid.DataSource.DataSet.Next;
        i:=i+1;
      end;
    //&micro;÷&Otilde;&ucirc;excel&Aacute;&ETH;&iquest;í&para;&Egrave;
      for i:=1 to colcount do
        e.Workbooks[1].Sheets[1].Columns[i].ColumnWidth:=columnwidth[i-1];  y1:=5;
      x1:=1;
      y2:=8+DBGrid.DataSource.DataSet.RecordCount-1;
      x2:=ColCount;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeTop].LineStyle := xlContinuous;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeTop].Weight := xlThin;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeTop].ColorIndex := xlAutomatic;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeBottom].LineStyle := xlContinuous;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeBottom].Weight := xlThin;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeBottom].ColorIndex := xlAutomatic;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeRight].LineStyle := xlContinuous;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeRight].Weight := xlThin;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeRight].ColorIndex := xlAutomatic;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeLeft].LineStyle := xlContinuous;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeLeft].Weight := xlThin;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlEdgeLeft].ColorIndex := xlAutomatic;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlInsideVertical].LineStyle := xlContinuous;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlInsideVertical].Weight := xlThin;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlInsideVertical].ColorIndex := xlAutomatic;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlInsideHorizontal].LineStyle := xlContinuous;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlInsideHorizontal].Weight := xlThin;
      e.Workbooks[1].Sheets[1].Range[e.Workbooks[1].Sheets[1].Cells[y1,x1],e.Workbooks[1].Sheets[1].Cells[y2,x2]].Borders[xlInsideHorizontal].ColorIndex := xlAutomatic;//  e.visible:=true;//  excel.DisplayAlerts:=true;//&Iacute;&Euml;&sup3;&ouml;&Ecirc;±&sup2;&raquo;&Igrave;á&Ecirc;&frac34;//  e.quit;
      

  4.   

    可以通过edit等相关的控件,可视化录入
    turbozzh(涡轮超人) 可行
    如V.Cells[1,4].Value := edit.text;