asp.net导出excel保存到D盘
各位大侠,请问c#中怎么把数据库里的数据导出到excel后,直接把excel保存到D盘中。请各位帮帮忙啊! --------------------编程问答-------------------- 怎么没人帮忙啊!!!!!!!!!!!!! --------------------编程问答-------------------- http://blog.csdn.net/tonyqus/archive/2009/11/29/4898453.aspx看看这个 --------------------编程问答-------------------- 你是想把从DB里查到的数据存到Excel里是吗?
我这里有段代码可以实现,不知道是不是你要的。。。
--------------------编程问答-------------------- Microsoft.Office.Interop.Excel.ApplicationClass excel = new ApplicationClass();
//将一个DataTable转换成Excel
//只要调用这个方法就可以了……
//dt是DataTable对象,FileName是文件的完整路径+文件名,Title是在Excel文件中第一行的标题!
public void CreateExcelDT(DataTable dt, string FileName,string Title)
{
StreamWriter sw = new StreamWriter(FileName, true, System.Text.Encoding.GetEncoding("gb2312"));
string colHeaders = "", ls_item = "";
DataRow[] myRow = dt.Select();//可以类似dt.Select("id>10")之形式达到数据筛选目的
int i = 0;
int cl = dt.Columns.Count;
//导出表头(在中间位置)
for (i = 0; i < cl; i++)
{
if (i == cl / 2)
{
sw.Write(Title);
}
if (i == (cl - 1))
{
sw.Write("\n");
}
else
{
sw.Write("\t");
}
}
//取得数据表各列标题,各标题之间以t分割,最后一个列标题后加回车符
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
colHeaders += dt.Columns[i].Caption.ToString() + "\n";
}
else
{
colHeaders += dt.Columns[i].Caption.ToString() + "\t";
}
}
sw.Write(colHeaders);
//向HTTP输出流中写入取得的数据信息
//逐行处理数据
foreach (DataRow row in myRow)
{
//当前行数据写入HTTP输出流,并且置空ls_item以便下行数据
for (i = 0; i < cl; i++)
{
if (i == (cl - 1))//最后一列,加n
{
ls_item += row[i].ToString() + "\n";
}
else
{
ls_item += row[i].ToString() + "\t";
}
}
sw.Write(ls_item);
ls_item = "";
}
sw.Close();
}
int rowIndex = 1;
int colIndex = 0;
Microsoft.Office.Interop.Excel._Workbook xBk;
Microsoft.Office.Interop.Excel._Worksheet xSt;
xBk = excel.Workbooks.Add(true);
xSt = (Microsoft.Office.Interop.Excel._Worksheet)xBk.ActiveSheet;
System.Data.DataTable data = ds.Tables[0];
for (int j = 0; j < data.Columns.Count;j++ )
{
colIndex++;
excel.Cells[1, colIndex] = data.Columns[j].ColumnName.ToString();
xSt.get_Range(excel.Cells[1, colIndex], excel.Cells[1, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
}
for (int i = 0; i < data.Rows.Count; i++)
{
rowIndex++;
colIndex =0;
for (int j = 0; j < data.Columns.Count; j++)
{
colIndex++;
excel.Cells[rowIndex, colIndex] = "" + data.Rows[i][j].ToString();
xSt.get_Range(excel.Cells[rowIndex, colIndex], excel.Cells[rowIndex, colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;
xSt.get_Range(excel.Cells[2, 2], excel.Cells[rowIndex, 2]).Select();
xSt.get_Range(excel.Cells[2, 2], excel.Cells[rowIndex, 2]).Interior.ColorIndex = 20;//设置为浅黄色,共计有56种
xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowIndex+1, colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowIndex + 1, colIndex]).ColumnWidth=20;
xSt.get_Range(excel.Cells[1, 1], excel.Cells[rowIndex + 1, colIndex]).RowHeight = 30;
xSt.get_Range(excel.Cells[2, 2], excel.Cells[rowIndex, 2]).ColumnWidth = 8;
}
}
excel.Visible = false;
xBk.Saved = true;
我只是想保存到指定的路径D盘,因为如果数据多的话打开Excel表的话,很慢,所以我就想直接保存。
xBk.Saved后面的我不知道怎么写了,帮帮忙啊 --------------------编程问答-------------------- 我记得我这段代码是直接保存的啊,要三个参数,应该就是可以的啊…… --------------------编程问答-------------------- 可是这个导出Excel表要对它的属性进行修改啊,如Excel表的颜色啊,长宽啊,但是5楼,你没有写,你保存到哪个路径下啊 --------------------编程问答--------------------
--------------------编程问答--------------------
//生成 Excel 文件 并保存到服务器上
private bool Excel(string strPath, string strFileName)
{
try
{
FileStream fs = new FileStream(strPath + strFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine("<HTML><HEAD>");
sw.WriteLine("<META HTTP-EQUIV='content-type' CONTENT='text/html; charset=GB2312'>");
sw.WriteLine("</HEAD><BODY>");
sw.WriteLine("<table cellSpacing='0' cellPadding='0' width ='100%' border='1'>");
DataTable dt = this.BindLogXinXi();
sw.WriteLine("<tr><td colspan=" + 5 + " align='center' style='font: bold 17px;'>日志表</td></tr>");
sw.WriteLine("<tr>");
sw.WriteLine("<td style='font: bold 14px;'>类型</td>");
sw.WriteLine("<td style='font: bold 14px;'>姓名</td>");
sw.WriteLine("<td style='font: bold 14px;'>操作内容</td>");
sw.WriteLine("<td style='font: bold 14px;'>登录IP</td>");
sw.WriteLine("<td style='font: bold 14px;'>时间</td>");
sw.WriteLine("</tr>");
foreach(DataRow dr in dt.Rows)
{
sw.WriteLine("<tr>");
//类型
if (!Convert.IsDBNull(dr["TypeName"]))
sw.Write("<td>" + dr["TypeName"].ToString() + "</td>");
else
sw.Write("<td> </td>");
//姓名
if (!Convert.IsDBNull(dr["XingMing"]))
sw.Write("<td>" + dr["XingMing"].ToString() + "</td>");
else
sw.Write("<td> </td>");
//操作内容
if (!Convert.IsDBNull(dr["Log_Content"]))
sw.Write("<td>" + dr["Log_Content"].ToString() + "</td>");
else
sw.Write("<td> </td>");
//登录IP
if (!Convert.IsDBNull(dr["Log_IP"]))
sw.Write("<td>" + dr["Log_IP"].ToString() + "</td>");
else
sw.Write("<td> </td>");
//时间
if (!Convert.IsDBNull(dr["LogDate"]))
sw.Write("<td>" + dr["LogDate"].ToString() + "</td>");
else
sw.Write("<td> </td>");
sw.WriteLine("</tr>");
}
sw.WriteLine("</table></BODY></HTML>");
sw.Close();
return true;
}
catch
{
return false;
}
}
//获得路径
string strPath = getPath("DaoChu_Log/");
//获得文件名
string strFileName = getFileName();
if (this.Excel(strPath, strFileName))
{
//写入日志
LogGuanLi.Add_Log(5, "导出日志信息");
this.Page.Response.Buffer = true;
this.Page.Response.Clear();
this.Page.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
this.Page.Response.ContentType = "application/octet-stream";
this.Page.Response.WriteFile(strPath + strFileName);
}
这个是调用。 --------------------编程问答-------------------- up!
补充:.NET技术 , C#