我做的系统中需要做一个财务报表,根据供应商不同要计算total,而且要在同一个excel中显示出来,就是说要做成如下的样式:supplier        QTY     
1                1
1                1
TOTAL 1          2
2                4
2                5
TOTAL 2          9
我想请教一下如何在程序中来控制total的输出

解决方案 »

  1.   

    肯定要放在一个EXCEL表中,这是财务人员要求的
      

  2.   

    两个表,先计算结果,先导入到EXCEL中第一个表(逐行导入),再导入第二个表,最后导入合计.OK?
      

  3.   

    看错了.你的这个需求和我现在在做的基本一样.不过只是我是根据某一列的值不同生成不同的EXCEL而已.==
    Public Sub CreateExcel(ByVal _strExcelPath As String, ByVal _ExcelDataSet As DataSet, ByVal _strColumnName As String)
            Dim xlSheet As Excel.Worksheet
            Dim xlBook As Excel.Workbook
            Dim xlDBTable As Excel.DataTable
            Dim strPath As String        Try
                '            '
                Dim arry As ArrayList
                arry = New ArrayList
                Dim dr As DataRow
                Dim i As Integer = 0
                Dim strAll As String
                '
                '
                '
                Dim dv As DataView
                dv = New DataView(_ExcelDataSet.Tables(0))
                '
                dv.Sort = _strColumnName
                Dim tmp As String
                Dim drv As DataRowView
                For Each drv In dv
                    If tmp <> drv.Row(_strColumnName) Then
                        tmp = Convert.ToString(drv.Row(_strColumnName))
                        arry.Add(tmp)
                    End If
                Next            Dim k As Integer = 0
                For k = 0 To arry.Count - 1
                    Dim xlApp As New Excel.Application
                    xlBook = xlApp.Workbooks.Add
                    xlSheet = xlBook.Worksheets("sheet1")
                    '&#63730;
                    '
                    strPath = _strExcelPath & "\" & k + 1 & ".xls"
                    Dim col As DataColumn
                    Dim colIndex As Integer = 0
                    For Each col In _ExcelDataSet.Tables(0).Columns
                        colIndex = colIndex + 1
                        '
                        xlApp.Cells(1, colIndex) = col.ColumnName
                    Next                Dim row As DataRow
                    Dim rowIndex As Integer = 0
                    '
                    For Each row In _ExcelDataSet.Tables(0).Select(_strColumnName & "='" & Convert.ToString(arry(k)) & "'")
                        '
                        rowIndex = rowIndex + 1
                        colIndex = 0
                        For Each col In _ExcelDataSet.Tables(0).Columns
                            colIndex = colIndex + 1
                            xlApp.Cells(rowIndex, colIndex) = row(col.ColumnName)
                        Next                Next                xlBook.SaveAs(strPath, xlApp.ActiveWorkbook.FileFormat, "", "", xlApp.ActiveWorkbook.ReadOnlyRecommended, xlApp.ActiveWorkbook.CreateBackup, Excel.XlSaveAsAccessMode.xlShared.xlShared, xlApp.ActiveWorkbook.ConflictResolution, False, "", "")                '
                    xlBook.Close()
                    xlApp.Quit()
                    xlApp = Nothing
                Next
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub这是我的实现.你看一下.原来还有注解的.不过原来用的是中文,后来改成日文都成乱码了.不过仔细看一下能看能的. 参数:将要存放EXCEL文件的路径;dataset;根据列分开生成EXCEL文件的列名.我要生成的文件按1.xls,2.xls......这样的文件名
      

  4.   

    楼上的不错。
    另外,楼主理解错我的意思了!我不是说生成两个EXCEL表,而是两个DataTable.
      

  5.   

    楼上的还是没理解我的意思,我的意思是我的excel文件中supplier的个数是不确定的,而supplier的记录数也是不确定的,所以不可能按supplier的个数来一个一个的做datatable,再进行join。准确地说我的需求就是:在A表中有我所有的纪录,我想对A表中每个SUPPLIER的各条记录进行计算,并显示在EXCLE文件该SUPPLIER的纪录的下面,请大家再指点指点
      

  6.   

    嗯,的确没有看清楼主的意思。我觉得有两种思路
    第一用存储过程:
    在存储过程要把你要的这个表处理好,然后返回记录集,再生成EXCEL.存储过程中写个临时表就可以了.
    第二是用水晶报表.这个就简单了.
      

  7.   

    谢谢楼上的,我用存储过程试试看,但是如何控制颜色呢,这又是一个比较郁闷的事情,就是我的total那一行的颜色必须跟其它的颜色不一样,有什么可行的办法吗?
      

  8.   

    用存储过程实现的方法太复杂了,而且输出结果也不好控制,能在程序中得到TOTAL的计算方法吗?
      

  9.   

    protected void Button1_Click(object sender, System.EventArgs e)
    {
    SqlConnection Conn=new SqlConnection(information.ConnectionString);
    Excel.Application  oExcel;  
    Excel.Workbook  oBook;  
    Object  oMissing  =  System.Reflection.Missing.Value;  
    oExcel  =  new  Excel.Application();  
    oBook  =  oExcel.Workbooks.Add(oMissing);  
    try
    {
    Conn.Open();
    SqlCommand Cmd=Conn.CreateCommand();
    Cmd.CommandType=CommandType.StoredProcedure;
    Cmd.CommandText="njpm";
    DataSet ds=new DataSet();
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand=Cmd;
    da.Fill(ds,"score");
    int rowIndex=1;
    int colIndex=0; DataTable table=ds.Tables["score"]; //将所得到的表的列名,赋值给单元格
    foreach(DataColumn col in table.Columns)
    {
    colIndex++; 
    oExcel.Cells[1,colIndex]=col.ColumnName;    
    } //同样方法处理数据
    foreach(DataRow row in table.Rows)
    {
    rowIndex++;
    colIndex=0;
    foreach(DataColumn col in table.Columns)
    {
    colIndex++;
    oExcel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
    }
    }
    oBook.Saved  =  true;  
    oExcel.UserControl  =  false;  
    string  mm=Server.MapPath(".")+"\\aa.xls";//服务器保存地址  
    oExcel.ActiveWorkbook.SaveCopyAs  (mm);  
    Response.Redirect  ("aa.xls");//注意上保存和调用时的路径。
    }
    catch (Exception exc) 
    {
    string msg="数据导出Excel时出现错误!";
    Response.Write("<SCRIPT   language='javascript'>");
    Response.Write("alert('"   +   msg   +   "')");
    Response.Write("</SCRIPT>");
    Response.Write(exc.Message);
    }
    finally
    {
    Conn.Close();
    oBook=null;
    oExcel.Quit();
    oExcel=null;
    GC.Collect(0);
    KillExcelProcess();
    }
    }
    private void KillExcelProcess()
    {
    System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessesByName("EXCEL")[0];
    p.Kill();
    }
      

  10.   

    第一个datatable循环,写入excel,然后将最后一行行号赋给参数a,下一个table继续循环,到最后一行再赋值给另外一个参数b,然后sum(Ba,Bb)就行了