目录
调用存储过程
cs
//调用存储过程生成Excel
string inforStr="";
conString = ConfigurationManager.ConnectionStrings["Con"].ConnectionString.ToString();
SqlConnection conn2 = new SqlConnection(conString);
conn2.Close();
conn2.Open();
//创建指令对象
SqlCommand sqlCommand2 = new SqlCommand("ConvertAccountingDocumentToExcel", conn2);
sqlCommand2.CommandType = CommandType.StoredProcedure; //存储过程,一定要写这一句,否则无法当做存储过程来执行
sqlCommand2.Parameters.Add("@gjahr", SqlDbType.Char, 4).Value = this.year;
sqlCommand2.Parameters.Add("@monat", SqlDbType.Char, 2).Value = this.month.PadLeft(2,'0');
sqlCommand2.Parameters.Add("@infor", SqlDbType.NVarChar,1000).Direction = ParameterDirection.Output; //返回参数
sqlCommand2.ExecuteNonQuery();
inforStr = sqlCommand2.Parameters["@infor"].Value.ToString(); //获取返回值
conn2.Close();
if (inforStr != "")
{
MessageBox.Show(inforStr);
}
C#配置文件

数据库存储过程
sql
USE [HS_Label_System]
GO
/****** Object: StoredProcedure [dbo].[ConvertAccountingDocumentToExcel] Script Date: 02/07/2025 09:05:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: LiuHongyu
-- Create date: 2024-02-06
-- Description: 转换会计凭证,导出Excel文件
-- =============================================
ALTER PROCEDURE [dbo].[ConvertAccountingDocumentToExcel]
-- Add the parameters for the stored procedure here
@gjahr as char(4),
@monat as char(2),
@infor as nvarchar(max) output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @sao int,
@xpcmd as int
SELECT @sao=convert(int,value) FROM sys.configurations where name='show advanced options' ORDER BY name
SELECT @xpcmd=convert(int,value) FROM sys.configurations where name = 'xp_cmdshell' ORDER BY name
if @sao = 0
begin
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
end
if @xpcmd = 0
begin
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE
end
SELECT * INTO ##Temp FROM AccountingDocument WHERE GJAHR = @gjahr AND MONAT = @monat
SELECT * FROM ##Temp
DECLARE @ReturnValue INT;
EXEC @ReturnValue = master..xp_cmdshell 'bcp "Select Top 1 N''公司编码'' as BUKRS,N''公司名称'' as BUTXT,N''会计年度'' as GJAHR,N''凭证编号'' as BELNR,N''凭证项目'' as BUZEI,N''记账期间'' as MONAT,N''凭证日期'' as BLDAT,N''过账日期'' as BUDAT,N''凭证类型'' as BLART,N''冲销凭证号'' as STBLG,N''摘要'' as SGTXT,N''过账码'' as BSCHL,N''科目编码'' as HKONT,N''科目名称'' as TXT20,N''对方科目编码'' as GKONT,N''对方科目名称'' as GKONT_1,N''业务伙伴编码'' as BANUM,N''业务伙伴名称'' as BADEC,N''资产编码'' as ANLN1,N''资产名称'' as TXT50,N''成本中心编码'' as KOSTL,N''成本中心名称'' as KTEXT,N''订单'' as AUFNR,N''内部订单名称'' as AUFTX,N''币别'' as WAERS,N''汇率'' as KURSF,N''方向'' as SHKZG,N''借方金额(原币)'' as SWRBTR,N''贷方金额(原币)'' as HWRBTR,N''借方金额(本币)'' as SDMBTR,N''贷方金额(本币)'' as HDMBTR,N''功能范围'' as RFAREA FROM HS_Label_System.dbo.AccountingDocument Union all SELECT BUKRS,BUTXT,GJAHR,BELNR,BUZEI,MONAT,CONVERT(varchar,BLDAT,120),CONVERT(varchar,BUDAT,120),BLART,STBLG,SGTXT,BSCHL,HKONT,TXT20,GKONT,GKONT_1,BANUM,BADEC,ANLN1,TXT50,KOSTL,KTEXT,AUFNR,AUFTX,WAERS,CONVERT(VARCHAR(20),KURSF),SHKZG,CONVERT(VARCHAR(30),SWRBTR),CONVERT(VARCHAR(30),HWRBTR),CONVERT(VARCHAR(30),SDMBTR),CONVERT(VARCHAR(30),HDMBTR),RFAREA FROM tempdb.dbo.##Temp" queryout "D:\Projects\Visual_Studio_2022\WindowsFormsApp1_SAP_RFC\AccountingDocument.csv" -w -S"localhost" -U"sa" -P"GDhs3225966"'
if @ReturnValue = 0
begin
set @infor = '创建Excel执行成功'
end
else
begin
set @Infor='创建EXCEL执行失败'
end
EXEC sp_configure 'xp_cmdshell', 0
RECONFIGURE
EXEC sp_configure 'show advanced options', 0
RECONFIGURE
--删除临时表
DROP TABLE ##Temp
-- Insert statements for procedure here
END