我有一个包含两个表的数据集。第一个告诉我它是一个doc文件。第二个包含文件的字节数组。但是我需要取出前85个字节,因为它们包含Access创建的OLE Object头。到目前为止我有这个。
protected void Page_Load(object sender, EventArgs e)
{
int examId = Convert.ToInt32(Request.QueryString["exam_id"]);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RHD_CS"].ConnectionString);
try
{
conn.Open();
}
catch (Exception)
{
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_spirometry_readexam";
cmd.Parameters.Add("@iExamID", SqlDbType.Int);
cmd.Parameters["@iExamID"].Value = examId;
string sFileName = "exam" + examId.ToString()+".";
DataSet dsData = new DataSet();
SqlDataAdapter daData = new SqlDataAdapter(cmd);
daData.Fill(dsData);
DataRow drDocData = dsData.Tables[1].Rows[0];
byte[] bDocData = (byte[])drDocData["exam_binary"];
Response.Clear();
Response.ContentType = "application/msword";
Response.AddHeader("Content-Disposition", "attachment;filename= filename.doc");
Response.BinaryWrite(bDocData);
Response.End();
这样可行,但是在下载文档时,它只是一堆乱码。
解决 感谢您的帮助。我通过使用流然后将其转换回字节来解决问题。
protected void Page_Load(object sender, EventArgs e)
{
int examId = Convert.ToInt32(Request.QueryString["exam_id"]);
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RHD_CS"].ConnectionString);
try
{
conn.Open();
}
catch (Exception)
{
}
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_spirometry_readexam";
cmd.Parameters.Add("@iExamID", SqlDbType.Int);
cmd.Parameters["@iExamID"].Value = examId;
string sFileName = "exam" + examId.ToString()+".";
DataSet dsData = new DataSet();
SqlDataAdapter daData = new SqlDataAdapter(cmd);
daData.Fill(dsData);
DataRow drDocData = dsData.Tables[1].Rows[0];
byte[] bDocData = (byte[])drDocData["exam_binary"];
Label1.Text = bDocData.Length.ToString();
Stream stream = new MemoryStream(bDocData);
stream.Position = 85;
bDocData = ReadFully(stream);
Response.Clear();
Response.ContentType = "application/msword";
Response.AddHeader("Content-Disposition", "attachment;filename= insert filename.doc");
Response.BinaryWrite(bDocData);
Response.End();
}
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
答案 0 :(得分:1)
试试这个
//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dsData.Tables[1];//Assign datatable to gridview source
GridView1.DataBind();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=DataTable.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-word ";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();