将这段vb.net代码改成c#.net代码~谢谢

代码如下:DimexcelAsExcel.ApplicationDimxBkAsExcel._WorkbookDimxStAsExcel._WorksheetDimxRa... 代码如下:
Dim excel As Excel.Application
Dim xBk As Excel._Workbook
Dim xSt As Excel._Worksheet
Dim xRange As Excel.Range
Dim xPivotCache As Excel.PivotCache
Dim xPivotTable As Excel.PivotTable
Dim xPivotField As Excel.PivotField
Dim cnnsr As String, sql As String
Dim RowFields() As String = {"", "", ""}
Dim PageFields() As String = {"", "", "", "", "", ""}

'SERVER 是服务器名或服务器的IP地址
'DATABASE 是数据库名
'Table 是表名

Try
' 开始导出
cnnsr = "ODBC;DRIVER=SQL Server;SERVER=" + SERVER
cnnsr = cnnsr + ";UID=;APP=Report Tools;WSID=ReportClient;DATABASE=" + DATABASE
cnnsr = cnnsr + ";Trusted_Connection=Yes"

excel = New Excel.ApplicationClass
xBk = excel.Workbooks.Add(True)
xSt = xBk.ActiveSheet

xRange = xSt.Range("A4")
xRange.Select()

' 开始
xPivotCache = xBk.PivotCaches.Add(SourceType:=2)
xPivotCache.Connection = cnnsr
xPivotCache.CommandType = 2

sql = "select * from " + Table

xPivotCache.CommandText = sql
xPivotTable = xPivotCache.CreatePivotTable(TableDestination:="Sheet1!R3C1", TableName:="数据透视表1", DefaultVersion:=1)

'准备行字段
RowFields(0) = "字段1"
RowFields(1) = "字段2"
RowFields(2) = "字段3"
'准备页面字段
PageFields(0) = "字段4"
PageFields(1) = "字段5"
PageFields(2) = "字段6"
PageFields(3) = "字段7"
PageFields(4) = "字段8"
PageFields(5) = "字段9"
xPivotTable.AddFields(RowFields:=RowFields, PageFields:=PageFields)

xPivotField = xPivotTable.PivotFields("数量")
xPivotField.Orientation = 4

' 关闭工具条
'xBk.ShowPivotTableFieldList = False
'excel.CommandBars("PivotTable").visible = False

excel.Visible = True

Catch ex As Exception
If cnn.State = ConnectionState.Open Then
cnn.Close()
End If
xBk.Close(0)
excel.Quit()
MessageBox.Show(ex.Message, "报表工具", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try

将之转换为c#,谢谢!完成后再加分!
展开
 我来答
egghead008
2011-04-29 · TA获得超过1164个赞
知道小有建树答主
回答量:1424
采纳率:100%
帮助的人:1383万
展开全部
Excel.Application excel = default(Excel.Application);
Excel._Workbook xBk = default(Excel._Workbook);
Excel._Worksheet xSt = default(Excel._Worksheet);
Excel.Range xRange = default(Excel.Range);
Excel.PivotCache xPivotCache = default(Excel.PivotCache);
Excel.PivotTable xPivotTable = default(Excel.PivotTable);
Excel.PivotField xPivotField = default(Excel.PivotField);
string cnnsr = null;
string sql = null;
string[] RowFields = {
"",
"",
""
};
string[] PageFields = {
"",
"",
"",
"",
"",
""
};

//SERVER 是服务器名或服务器的IP地址
//DATABASE 是数据库名
//Table 是表名

try {
// 开始导出
cnnsr = "ODBC;DRIVER=SQL Server;SERVER=" + SERVER;
cnnsr = cnnsr + ";UID=;APP=Report Tools;WSID=ReportClient;DATABASE=" + DATABASE;
cnnsr = cnnsr + ";Trusted_Connection=Yes";

excel = new Excel.ApplicationClass();
xBk = excel.Workbooks.Add(true);
xSt = xBk.ActiveSheet;

xRange = xSt.Range("A4");
xRange.Select();

// 开始
xPivotCache = xBk.PivotCaches.Add(SourceType: 2);
xPivotCache.Connection = cnnsr;
xPivotCache.CommandType = 2;

sql = "select * from " + Table;

xPivotCache.CommandText = sql;
xPivotTable = xPivotCache.CreatePivotTable(TableDestination: "Sheet1!R3C1", TableName: "数据透视表1", DefaultVersion: 1);

//准备行字段
RowFields[0] = "字段1";
RowFields[1] = "字段2";
RowFields[2] = "字段3";
//准备页面字段
PageFields[0] = "字段4";
PageFields[1] = "字段5";
PageFields[2] = "字段6";
PageFields[3] = "字段7";
PageFields[4] = "字段8";
PageFields[5] = "字段9";
xPivotTable.AddFields(RowFields: RowFields, PageFields: PageFields);

xPivotField = xPivotTable.PivotFields("数量");
xPivotField.Orientation = 4;

// 关闭工具条
//xBk.ShowPivotTableFieldList = False
//excel.CommandBars("PivotTable").visible = False

excel.Visible = true;

} catch (Exception ex) {
if (cnn.State == ConnectionState.Open) {
cnn.Close();
}
xBk.Close(0);
excel.Quit();
MessageBox.Show(ex.Message, "报表工具", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
更多追问追答
追问
Excel.Application
需要using什么????
追答
需要,你自己检查一下吧
百度网友f71e2e86f
2011-04-29
知道答主
回答量:13
采纳率:0%
帮助的人:0
展开全部
直接找一个代码转换工具,自己接解决了
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
gotocommon
2011-04-29
知道答主
回答量:21
采纳率:0%
帮助的人:14.2万
展开全部
授你以鱼不如授你以渔,直接搜下相关工具
要从根本上解决还得两样语言都会点
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式