我使用下面的代码将sql数据转换为excel工作簿...使用此代码.xls文件
在C://
驱动器上创建。如何让它可以下载,客户端可以下载吗?
帮我。我在使用带有c#的asp.net。下面的代码适用于存储.xls文件
c:/ drive。
try
{
myfilename = subject + "_Sessional" + sessional + "_Batch" + batch;
string sql = null;
string data = null;
int i = 1;
int j = 0;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Id";
xlWorkSheet.Cells[1, 2] = "RollNo";
xlWorkSheet.Cells[1, 3] = "Name";
xlWorkSheet.Cells[1, 4] = "Branch";
xlWorkSheet.Cells[1, 5] = "Sem";
xlWorkSheet.Cells[1, 6] = "Year";
xlWorkSheet.Cells[1, 7] = "Batch";
xlWorkSheet.Cells[1, 8] = "Subject";
xlWorkSheet.Cells[1, 9] = "Sessional";
xlWorkSheet.Cells[1, 10] = "Attendence";
xlWorkSheet.Cells[1, 11] = "OutOff";
using (cnn = new SqlConnection(strConnString))
{
cnn.Open();
sql = "SELECT DISTINCT [st_id], [st_rollno], [st_name], [branch_name], [sem_no], [batch_year] FROM [Student] WHERE (([st_rollno] >=@st_rollno) AND ([st_rollno] <=@st_rollno2) AND ([sem_no] = @sem_no) AND ([branch_name] = @branch_name)) ORDER BY [st_rollno]";
SqlDataAdapter dscmd = new SqlDataAdapter();
SqlCommand selectCMD = new SqlCommand(sql, cnn);
dscmd.SelectCommand = selectCMD;
selectCMD.Parameters.Add("@st_rollno", SqlDbType.VarChar, 10).Value = firstrollno.Text;
selectCMD.Parameters.Add("@st_rollno2", SqlDbType.VarChar, 10).Value = lastrollno.Text;
selectCMD.Parameters.Add("@sem_no", SqlDbType.Int).Value = dpsemno.SelectedItem.Text.ToString();
selectCMD.Parameters.Add("@branch_name", SqlDbType.VarChar, 5).Value = dpbranchname.SelectedItem.Text.ToString();
DataSet ds = new DataSet();
dscmd.Fill(ds);
for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
for (j = 0; j <= ds.Tables[0].Columns.Count - 1; j++)
{
data = ds.Tables[0].Rows[i].ItemArray[j].ToString();
xlWorkSheet.Cells[i + 2, j + 1] = data;
xlWorkSheet.Cells[i + 2, 7] = batch;
xlWorkSheet.Cells[i + 2, 8] = subject;
xlWorkSheet.Cells[i + 2, 9] = sessional;
xlWorkSheet.Cells[i + 2, 11] = outoff;
}
}
xlWorkBook.SaveAs("c:\\" + myfilename + ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
lbmsg.Text = "Excel file created,you can find the file in c:\\";
lbmsg.ForeColor = Color.Green;
lbmsg.Visible = true;
}
}
catch (SqlException ee)
{
lbmsg.Text = ee.Message;
lbmsg.ForeColor = Color.Green;
lbmsg.Visible = true;
}
finally
{
cnn.Close();
}
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
Label2.Text= "Exception Occured while releasing object " + ex.ToString();
Label2.ForeColor = Color.Green;
Label2.Visible = true;
}
}
答案 0 :(得分:0)
非常简单,将此代码放在下载按钮上单击
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "application/vnd.xls";
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.End();
希望这有效。