我做的系统中需要做一个财务报表,根据供应商不同要计算total,而且要在同一个excel中显示出来,就是说要做成如下的样式:supplier QTY
1 1
1 1
TOTAL 1 2
2 4
2 5
TOTAL 2 9
我想请教一下如何在程序中来控制total的输出
1 1
1 1
TOTAL 1 2
2 4
2 5
TOTAL 2 9
我想请教一下如何在程序中来控制total的输出
解决方案 »
- gridview分页后怎么计算整个gridview多少行
- “/xueshen2”应用程序中的服务器错误。用户 'XGN-7733F37120F\ASPNET' 登录失败。是什么原因?
- C#中的自定义对象怎么释放?
- 为什么在编译的时候没有问题,可运行debug下面的EXE时就回报错,怎么解决呢?谢谢
- Runtime到底是什么意思?
- 请问各位,我想点击treeView的节点后弹出一个窗口,应该怎么写事件啊
- 如何执行字符串中的代码(急)
- c#和access存储过程
- c# winform 如何判断 文本框中的值 是否被修改过
- 在同一个项目中,能同时使用c#类和VB类吗?
- comboBox控件的问题--怎样让comboBox显示时有一个默认值?
- 100分问一个程序部署问题.顶者有分.
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")
'
'
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......这样的文件名
另外,楼主理解错我的意思了!我不是说生成两个EXCEL表,而是两个DataTable.
第一用存储过程:
在存储过程要把你要的这个表处理好,然后返回记录集,再生成EXCEL.存储过程中写个临时表就可以了.
第二是用水晶报表.这个就简单了.
{
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();
}