真的好奇怪
excel的设置问题?

解决方案 »

  1.   

    那如果是excel的设置问题,该如何解决呢?楼上那位兄弟有何高见?/?
      

  2.   

    我想应该不是Excel的问题,你最好将Database中的数据完整显示出来,再查查原因!
      

  3.   

    给你个实例:
                      /// <summary>
    /// 建立Excel
    /// </summary>
    public void CreateExcel()
    {
    int rowIndex=4;//行起始坐标
    int colIndex=1;//列起始坐标
    ApplicationClass myApp=null;
    Workbook myBook=null;
    Worksheet mySheet=null;
    //如果文件不存在,则将模板文件拷贝一份作为输出文件
    //这里如果通过File.Create来创建文件是不行的,因为xls
    //的空文件也有固定的格式,跟文本不一样的,也许有其它
    //通过程序直接生成excel的方法
    if(!File.Exists(m_outFilePath))
    {
    if((File.GetAttributes(m_inputFilePath) & FileAttributes.ReadOnly)==FileAttributes.ReadOnly)
    {
       File.SetAttributes(m_inputFilePath,FileAttributes.Archive);
    }

    File.Copy(m_inputFilePath,m_outFilePath,true);
    } if((File.GetAttributes(m_outFilePath) & FileAttributes.ReadOnly)==FileAttributes.ReadOnly)
    {
    File.SetAttributes(m_outFilePath,FileAttributes.Archive);
    }
    myApp= new ApplicationClass();
    myApp.Visible=false;
    object oMissiong=System.Reflection.Missing.Value;
    myApp.Workbooks.Open(m_outFilePath,oMissiong,oMissiong,oMissiong,oMissiong,
    oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);//这个函数只适合于OFFICE2000回为它只有13个参数,要适应于OFFICE XP的话,得要15个参数
    myBook=myApp.Workbooks[1];
    mySheet=(Worksheet)myBook.ActiveSheet; //
    //取得标题
    //
    foreach(DataColumn col in m_dv.Table.Columns)
    {
    colIndex++;
    mySheet.Cells[4,colIndex] = col.ColumnName;
    mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
    //设置标题格式为居中对齐
    }
    //
    //取得表格中的数据
    //
    foreach(DataRowView row in m_dv)
    {
    rowIndex ++;
    colIndex = 1;
    foreach(DataColumn col in m_dv.Table.Columns)
    {
    colIndex ++;
    if(col.DataType == System.Type.GetType("System.DateTime"))
    {
    mySheet.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
    mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置日期型的字段格式为居中对齐
    }
    else
    if(col.DataType == System.Type.GetType("System.String"))
    {
    mySheet.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
    mySheet.get_Range(mySheet.Cells[rowIndex,colIndex],mySheet.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//设置字符型的字段格式为居中对齐
    }
    else
    {
    mySheet.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
    }
    }
    }
    //
    //加载一个合计行
    //
    int rowSum = rowIndex + 1;
    int colSum = 2; //
    //设置选中的部分的颜色
    //
    mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Select();
    mySheet.get_Range(mySheet.Cells[rowSum,colSum],mySheet.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//设置为浅黄色,共计有56种
    //
    //取得整个报表的标题
    //
    mySheet.Cells[2,2] = m_title;
    //
    //设置整个报表的标题格式
    //
    mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Bold = true;
    mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,2]).Font.Size = 22;
    //
    //设置报表表格为最适应宽度
    //
    mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Select();
    mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Columns.AutoFit();
    //
    //设置整个报表的标题为跨列居中
    //
    mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).Select();
    mySheet.get_Range(mySheet.Cells[2,2],mySheet.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
    //
    //绘制边框
    //
    mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
    mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//设置左边线加粗
    mySheet.get_Range(mySheet.Cells[4,2],mySheet.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//设置上边线加粗
    mySheet.get_Range(mySheet.Cells[4,colIndex],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//设置右边线加粗
    mySheet.get_Range(mySheet.Cells[rowSum,2],mySheet.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//设置下边线加粗
    myBook.Save();
    myBook.Close( true,m_outFilePath,true);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);
        
    GC.Collect();

    }
      

  4.   

    呵呵,谢谢各位了。我也跟大家说明咋回事吧。DB中那个栏位只要给歌空格就ok了^_^
      

  5.   

    搞什么东东,  是不是大妹子 跟 Crystal???