将数据库表导出到现有的excel文件

时间:2015-05-07 15:42:01

标签: sql excel

我已经看过了,但到目前为止我发现的是导出到现有的Excel工作表,我需要的是制作我现有的excel模板的副本并将数据传递到那里。我只需要以正确的方式指出,希望我足够清楚。

修改 我有一个应用于Java的应用程序,我将添加它。

3 个答案:

答案 0 :(得分:0)

假设您询问SQL Server,您可以创建两步作业,一个用于复制xls,第二步使用openrowset / insert复制到复制的Excel文件中。 这是Tsql复制模板并发送电子邮件,以及有关xp_cmdshell https://msdn.microsoft.com/en-us/library/ms190693.aspx的信息


    DECLARE @cmd varchar(512)
    DECLARE @fd varchar(512)
    DECLARE @odbc varchar(128)
    DECLARE @db varchar(128)
    SELECT @fd = 'D:\Reports\Exact\ByWeek' + CONVERT(VARCHAR(10),                 GETDATE()-8,120)+'_'+CONVERT(VARCHAR(10), GETDATE(),120)+'.xls'
    SELECT @cmd = 'copy D:\Reports\System\shipments_week.xls ' + @fd

    EXEC MASTER..XP_CMDSHELL @cmd, NO_OUTPUT

    SET @odbc = 'Microsoft.Jet.OLEDB.4.0' 
    SET @db = 'Excel 8.0;Database=' + @fd



    exec('INSERT INTO OPENrowset(''' + @odbc + ''',''' + @db + ''',''SELECT 
  OrderNr,Debtor,SUM_NSHIPPED,SUM_TOTAL,PERC_NSHIPPED,ORDDAT,INVDAT,ORD_SYSCREATED,HIST_LAST_MODIFIED,PT_WZ_SENT
FROM [Shipped$]'')
SELECT * FROM salag_shipmentsbyWeek')

    exec('INSERT INTO OPENrowset(''' + @odbc + ''',''' + @db + ''',''SELECT 
OrderNr,Debtor,SUM_NSHIPPED,SUM_TOTAL,PERC_NSHIPPED,ORDDAT,INVDAT,ORD_SYSCREATED,HIST_LAST_MODIFIED,PT_WZ_SENT
FROM [SB$]'')
SELECT * FROM salag_shipmentsbyWeek WHERE Debtor NOT LIKE ''%CASTORAMA%'' AND Debtor NOT LIKE ''Praktiker%''')


DECLARE @Body VARCHAR(4096)
DECLARE @BodyType VARCHAR(16)
DECLARE @path VARCHAR(128)
DECLARE @f VARCHAR(32)
DECLARE @topic VARCHAR(128)
SELECT @f = CONVERT(VARCHAR(10), GETDATE()-8,120)+'_'+CONVERT(VARCHAR(10), GETDATE(),120)
SELECT @topic = 'New Report ['+ CONVERT(VARCHAR(10), GETDATE()-8,120)+'_'+CONVERT(VARCHAR(10), GETDATE(),120) + ']'
SELECT @path = '\\appsrv\Reports\Exact\ByWeek' + CONVERT(VARCHAR(10), GETDATE()-8,120)+'_'+CONVERT(VARCHAR(10), GETDATE(),120)+'.xls'
SELECT @Body = 'New Scan Report has been created: '+@f+'   '+ @path
SELECT @BodyType = 'HTMLBody'

exec sp_send_cdosysmail 'Reports Info ','reports@foo ar.com',@topic, @Body, @BodyType

答案 1 :(得分:0)

我认为这篇文章很有用: Exporting DataTable to Excel in C#

答案 2 :(得分:0)

我能够使用JExcel实现我想要的东西,而且非常简单,在这里我将分享我的代码并希望它在将来帮助某人。对不起它尚未正确评论,我稍后会发布更新。

Db_Connect  connection = new Db_Connect();
 conn = connection.connect();



 String originalFile = "C:\\Users\\Geni\\Desktop\\Book1-Template-new.xls";   
 date = date.replace("/", "-");
 SimpleDateFormat myDate = new SimpleDateFormat("yyyy-MM-dd");

 try{
        Workbook original = Workbook.getWorkbook(new File(originalFile));
        WritableWorkbook copy = Workbook.createWorkbook(new File(date+".xls"), original);

        java.util.Date format = myDate.parse(date);
        java.sql.Date newDate = new java.sql.Date(format.getTime());

        String sql = "SELECT Name, sporecount.* FROM sporesfungi "
        + "INNER JOIN sporecount ON sporesfungi.IDSpore = sporecount.IDSpore"
                + "WHERE Date = ? ORDER BY TraceNum";

        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setDate(1, newDate);
        rs = statement.executeQuery();
        /*stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);*/


        WritableSheet sheet = copy.getSheet(0);
        WritableCell cell;
        String spore;

        while(rs.next()){
            for(int i = 2; i < 64 ;i++){
                cell = sheet.getWritableCell(1,i);
                spore =  cell.getContents();
                if(rs.getString("Name").equals(spore)){
                   Number l1 = new Number(14-rs.getInt("TraceNum"),i,rs.getInt("Amount")) ;
                   sheet.addCell(l1);

                    }

            }
        }


        copy.write();
        copy.close();
        original.close();
    }
    catch (BiffException | IOException e) {
    }