从数据库生成Excel / pdf报告 [英] Generating Excel / pdf reports from database

查看:137
本文介绍了从数据库生成Excel / pdf报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



请帮我用C#从bataBase中获得的数据创建excel表或pdf。



谢谢advance

解决方案

您好,



如果您使用的是Web App手段



使用此



 protected void btnExport_click(object sender,EventArgs e)
{
DataTable dt = objDataLayer.getEmployeeDataTable();
ExporttoExcel(dt,EmployeeList)
}

private void ExporttoExcel(DataTable table,string filename)
{
HttpContext.Current.Response。明确();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = true;
// HttpContext.Current.Response.ContentType =application / ms-excel; // 2003格式
HttpContext.Current.Response.ContentType =application / vnd.openxmlformats-officedocument.spreadsheetml.sheet ;

HttpContext.Current.Response.Write(@);
//HttpContext.Current.Response.AddHeader (\"Content-Disposition,attachment; filename = InvoiceList.xls) ; // 2003格式
HttpContext.Current.Response.AddHeader(Content-Disposition,attachment; filename =+ filename +。xls);

HttpContext.Current。 Response.Charset =utf-8;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(windows-1250);
HttpContext.Current.Response.Write( < font style =font-size:12.0pt; font-family:Calibri;>);
HttpContext.Current.Response.Write(< br>< br>< br> );
HttpContext.Current.Response.Write(< table border =1bgcolor =#ffffffbordercolor =#000000cellspacing =2pxcellpadding =2pxstyle =font -size:12.0pt; font-family:Calibri; background:white;>< tr>);

// int columnscount = grdInvoice.Co lumns.Count;

int columnscount = table.Columns.Count;

for(int j = 0; j< columnscount; j ++)
{
HttpContext.Current.Response.Write(< td>);
HttpContext.Current.Response.Write();
// HttpContext.Current.Response.Write(grdInvoice.Columns [j] .HeaderText.ToString());
HttpContext.Current.Response.Write(table.Columns [j] .ColumnName);
HttpContext.Current.Response.Write(
);
HttpContext.Current.Response.Write(< / td>);
}
HttpContext.Current.Response.Write(< / tr>);
foreach(table.Rows中的DataRow行)
{
HttpContext.Current.Response.Write(< tr>);
for(int i = 0; i< table.Columns.Count; i ++)
{
HttpContext.Current.Response.Write(< td>);
HttpContext.Current.Response.Write(row [i] .ToString());
HttpContext.Current.Response.Write(< / td>);
}

HttpContext.Current.Response.Write(< / tr>);
}
HttpContext.Current.Response.Write(< / table>);
HttpContext.Current.Response.Write(< / br>< / br>< / br>< / font>);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}





它会将导出的excel文件写入响应 - 表示在单击export时下载文件


您好b $ b

您可以尝试这个解决方案 http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm [ ^ ]



当我从中读取时,它应该有助于我发现它很有用excel文件中的单元格并将它们写入数据库,我用它来记录数据库查询中的记录到excel文件。它帮助了我很多。我希望它也能为你做同样的事。


你好b $ b代码窗口形式



  private   void  btnExport_Click( object  sender,EventArgs e)
{
Workbook book = new Workbook();
工作表表= book.Worksheets.Add( 示例);
WorksheetRow Wrow = sheet.Table.Rows.Add();
for int i = 1 ; i < dgvTasks.Columns.Count; i ++)
{
sheet.Table.Columns.Add( new WorksheetColumn( 100 ));
if (dgvTasks.Columns [i] .Visible!= false
{
Wrow.Cells.Add( new WorksheetCell(dgvTasks.Columns [i] .HeaderText.ToString()));
}
}
// 将每行和每列值存储到excel表

for int i = 1 ; i < = dgvTasks.Rows.Count; i ++)
{
WorksheetRow wrow = sheet。 Table.Rows.Add();

for int j = 1 ; j < dgvTasks.Columns.Count; j ++)
{
if (dgvTasks.Columns [j] .Visible!= false
{
wrow.Cells.Add(dgvTasks。行[i - 1 ]。Cells [j] .Value.ToString());
}
}
}

// save应用程序
尝试
{
随机R = new Random();
int num = R.Next();
string strFileName = c:\ \ output + num.ToString()+ 。xls;
saveFD.Filter = Excel文件(* .xls)| * .xls | txt文件(*。 txt)| * .txt |所有文件(*。*)| *。*;
saveFD.FilterIndex = 1 ;
saveFD.Title = 保存文件;
saveFD.RestoreDirectory = true ;

if (saveFD.ShowDialog()== DialogResult.OK)
{
strFileName = saveFD.FileName;

}
book.Save(strFileName.ToString());

}
catch
{
}
}


Hi,
Please help me in creating excel sheet or pdf from the data obtained in the bataBase using C#.

Thanks in advance

解决方案

Hi,

If Your are using Web App Means

Use this

protected void btnExport_click(object sender, EventArgs e)
{
   DataTable dt = objDataLayer.getEmployeeDataTable();
   ExporttoExcel(dt,"EmployeeList")
}

 private void ExporttoExcel(DataTable table,string filename)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.Buffer = true;
            //  HttpContext.Current.Response.ContentType = "application/ms-excel";//2003 format
            HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

            HttpContext.Current.Response.Write(@");
            //HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=InvoiceList.xls");// 2003 format
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+ filename +".xls");

            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
            HttpContext.Current.Response.Write("<font style="font-size:12.0pt; font-family:Calibri;">");
            HttpContext.Current.Response.Write("<br><br><br>");
            HttpContext.Current.Response.Write("<table border="1" bgcolor="#ffffff" bordercolor="#000000" cellspacing="2px" cellpadding="2px" style="font-size:12.0pt; font-family:Calibri; background:white;"> <tr>");

            //  int columnscount = grdInvoice.Columns.Count;

            int columnscount = table.Columns.Count;

            for (int j = 0; j < columnscount; j++)
            {
                HttpContext.Current.Response.Write("<td>");
                HttpContext.Current.Response.Write("");
                // HttpContext.Current.Response.Write(grdInvoice.Columns[j].HeaderText.ToString());
                HttpContext.Current.Response.Write(table.Columns[j].ColumnName);
                HttpContext.Current.Response.Write("");
                HttpContext.Current.Response.Write("</td>");
            }
            HttpContext.Current.Response.Write("</tr>");
            foreach (DataRow row in table.Rows)
            {
                HttpContext.Current.Response.Write("<tr>");
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    HttpContext.Current.Response.Write("<td>");
                    HttpContext.Current.Response.Write(row[i].ToString());
                    HttpContext.Current.Response.Write("</td>");
                }

                HttpContext.Current.Response.Write("</tr>");
            }
            HttpContext.Current.Response.Write("</table>");
            HttpContext.Current.Response.Write("</br></br></br></font>");
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }



it will write exported excel file to response - Means File being download when you click export


Hi
You can try this solution http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm[^]

It should help i found it useful when i read from cells within excel file and write them into a database and i used that to, write down records from a database query to an excel file. It helped me out a lot. I hope it will do the same for you too.


Hi the code windows form

private void btnExport_Click(object sender, EventArgs e)
        {
            Workbook book = new Workbook();
            Worksheet sheet = book.Worksheets.Add("Sample");
            WorksheetRow Wrow = sheet.Table.Rows.Add();
            for (int i = 1; i < dgvTasks.Columns.Count; i++)
            {
                sheet.Table.Columns.Add(new WorksheetColumn(100));
                if (dgvTasks.Columns[i].Visible != false)
                {
                    Wrow.Cells.Add(new WorksheetCell(dgvTasks.Columns[i].HeaderText.ToString()));
                }
            }
            // storing Each row and column value to excel sheet 

            for (int i = 1; i <= dgvTasks.Rows.Count; i++)
            {
                WorksheetRow wrow = sheet.Table.Rows.Add();

                for (int j = 1; j < dgvTasks.Columns.Count; j++)
                {
                    if (dgvTasks.Columns[j].Visible != false)
                    {
                        wrow.Cells.Add(dgvTasks.Rows[i - 1].Cells[j].Value.ToString());
                    }
                }
            }

            // save the application 
            try
            {
                Random R = new Random();
                int num = R.Next();
                string strFileName = "c:\\output" + num.ToString() + ".xls";
                saveFD.Filter = "Excel files (*.xls)|*.xls|txt files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFD.FilterIndex = 1;
                saveFD.Title = "Save file";
                saveFD.RestoreDirectory = true;

                if (saveFD.ShowDialog() == DialogResult.OK)
                {
                    strFileName = saveFD.FileName;

                }
                book.Save(strFileName.ToString());

            }
            catch
            {
            }
        }


这篇关于从数据库生成Excel / pdf报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆