把数据倒出到Excel,怎样设置某个单元格的格式为货币型且以“¥”开头?最好保留两位小数。
也就是把。数据0.30倒入到Excel,在Excel中显示¥0.30,且单元格格式是货币型
我倒入以后发现显示也是¥0.30,但单元格左上角有个很小的绿色三角号,鼠标移上去有个叹号,且不能累加。我想应该是没有辨别该单元格的格式。
sheet[count, 4].Value="¥"+Convert.ToDouble(myCurrentrow111["Call_Pay"]).ToString"0.00");
会出现此情况。

解决方案 »

  1.   

    设置一下NumberFormatLocal,在excel中录一个宏看一下
      

  2.   

    http://dotnet.aspx.cc/ShowDetail.aspx?id=8A4CBF47-B888-4832-3389-ED3A3A3C8AAB
      

  3.   

    using System;
    using System.Data;
    using Excel;namespace LogicLayer
    {
     /// <summary>
     /// OutputExcel 的摘要说明
     /// </summary>
     public class OutputExcel
     {
      public OutputExcel(DataView dv,string str)
      {
       //
       // TODO: 在此处添加构造函数逻辑
       //
       Excel.Application excel;
       int rowIndex=4;
       int colIndex=1;   Excel._Workbook xBk;
       Excel._Worksheet xSt;   excel= new Excel.ApplicationClass();;
       xBk = excel.Workbooks.Add(true);
       xSt = (Excel._Worksheet)xBk.ActiveSheet;   //
       //取得标题
       //
       foreach(DataColumn col in dv.Table.Columns)
       {
        colIndex++;
        excel.Cells[4,colIndex] = col.ColumnName;
        xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[4,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置标题格式为居中对齐
       }   //
       //取得表格中的数据
       //
       foreach(DataRowView row in dv)
       {
        rowIndex ++;
        colIndex = 1;
        foreach(DataColumn col in dv.Table.Columns)
        {
         colIndex ++;
         if(col.DataType == System.Type.GetType("System.DateTime"))
         {
          excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
          xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
         }
         else
         if(col.DataType == System.Type.GetType("System.String"))
         {
          excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
          xSt.get_Range(excel.Cells[rowIndex,colIndex],excel.Cells[rowIndex,colIndex]).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
         }
         else
         {
          excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
         }
        }
       }
       //
       //加载一个合计行
       //
       int rowSum = rowIndex + 1;
       int colSum = 2;
       excel.Cells[rowSum,2] = "合计";
       xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,2]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
       //
       //设置选中的部分的颜色
       //
       xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Select();
       xSt.get_Range(excel.Cells[rowSum,colSum],excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
       //
       //取得整个报表的标题
       //
       excel.Cells[2,2] = str;
       //
       //设置整个报表的标题格式
       //
       xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
       xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
       //
       //设置报表表格为最适应宽度
       //
       xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Select();
       xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Columns.AutoFit();
       //
       //设置整个报表的标题为跨列居中
       //
       xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).Select();
       xSt.get_Range(excel.Cells[2,2],excel.Cells[2,colIndex]).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection;
       //
       //绘制边框
       //
       xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
       xSt.get_Range(excel.Cells[4,2],excel.Cells[rowSum,2]).Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThick;//设置左边线加粗
       xSt.get_Range(excel.Cells[4,2],excel.Cells[4,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThick;//设置上边线加粗
       xSt.get_Range(excel.Cells[4,colIndex],excel.Cells[rowSum,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThick;//设置右边线加粗
       xSt.get_Range(excel.Cells[rowSum,2],excel.Cells[rowSum,colIndex]).Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThick;//设置下边线加粗
       //
       //显示效果
       //
       excel.Visible=true;
      }
     }
    }这是一个导出为Excel的类,它有两个参数,一个是数据源DataView,一个是大标题string
      

  4.   

    使用宏    Range("E8").Select
        Selection.NumberFormatLocal = "¥#,##0.00;¥-#,##0.00"
    这样就可以了,改动这里的E8即可
      

  5.   

    设置单元格(i行,j列),的格试:(看起来很乱,但是正确的,我试过)
    ((Excel.Range )(excel.Cells [i,j])).NumberFormatLocal = "¥#,##0.00_);[红色](¥#,##0.00)";
    /////////
    一定要给分哟!!别看简单,我做了测试程序,用了不少时间.