将Excel字节转换为PDF

时间:2013-10-04 10:22:44

标签: c# asp.net sql-server excel pdf

首先,我将Excel文档转换为字节并将它们存储在SQL SERVER 2008

string[] TotalCount = Directory.GetFiles(FileDropLocation);

foreach (string item in TotalCount)
{
    FileStream fs = new FileStream(item, FileMode.Open, FileAccess.Read);
    string fileName = Path.GetFileName(item);
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);

    //insert the file into database
    string strQuery = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
    SqlCommand cmd = new SqlCommand(strQuery);
    cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = fileName;
    cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;
    cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
    InsertUpdateData(cmd);
    lblMessage.ForeColor = System.Drawing.Color.Green;
    lblMessage.Text = "File Uploaded Successfully";

}

其次,我希望将其检索为PDF而不是excel并在我的aspx页面上显示它。 可以这样做吗? 请在此部分获得帮助。

先谢谢, HRG

2 个答案:

答案 0 :(得分:0)

如果您可以将Microsoft Excel安装到Web服务器上,则可以使用Excel's save as PDF功能进行转换。这使您可以非常轻松地基于Excel配置和输出PDF。它不会弹出服务器上的Excel窗口;)

答案 1 :(得分:0)

您希望按以下方式执行某些操作:

  1. 从SQL获取数据。
  2. 使用某种组件转换为PDF,例如http://www.doc2pdf.net/Excel2PDF/
  3. 在回复中将PDF数据发送到浏览器,例如Return PDF to WebPage from ASHX