程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#將數據導出到Execl匯總(2)

C#將數據導出到Execl匯總(2)

編輯:關於C語言

3、將DataSet中的數據導出Execl

有了上邊的思路,就是將在導出的信息,輸出(Response)客戶端,這樣就可以導出了。那麼把DataSet中的數據導出,也就是把DataSet中的表中的各行信息,以ms-Excel的格式Response到http流,這樣就OK了。說明:參數ds應為填充有數據表的DataSet,文件名是全名,包括後綴名,如execl2006.xls

public void CreateExcel(DataSet ds,string FileName)
{
HttpResponse resp;
resp = Page.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename="+FileName);
string colHeaders= "", ls_item="";
//定義表對象與行對象,同時用DataSet對其值進行初始化
DataTable dt=ds.Tables[0];
DataRow[] myRow=dt.Select();//可以類似dt.Select("id>10")之形式達到數據篩選目的
int i=0;
int cl=dt.Columns.Count;
//取得數據表各列標題,各標題之間以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";
}
}
resp.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";
}
}
resp.Write(ls_item);
ls_item="";
}
resp.End();
}

4、將datavIEw導出execl

若想實現更加富於變化或者行列不規則的execl導出時,可用本法。

public void OutputExcel(DataVIEw dv,string str)
{
//dv為要輸出到Excel的數據,str為標題名稱
GC.Collect();
Application Excel;// = new Application();
int rowIndex=4;
int colIndex=1;
_Workbook xBk;
_Worksheet xSt;
Excel= new ApplicationClass();
xBk = Excel.Workbooks.Add(true);
xSt = (_Worksheet)xBk.ActiveSheet;
//
//取得標題
//
foreach(DataColumn col in dv.Table.Columns)
{
colIndex++;
Excel.Cells[4,colIndex] = col.ColumnName;
xSt.get_Range(excel.Cells[4,colIndex],Excel.Cells[4,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//設置標題格式為居中對齊
}
//
//取得表格中的數據
//
foreach(DataRowVIEw row in dv)
{
rowIndex ++;
colIndex = 1;
foreach(DataColumn col in dv.Table.Columns)
{
colIndex ++;
if(col.DataType == System.Type.GetType("System.DateTime"))
{
Excel.Cells[rowIndex,colIndex] = (Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd");
xSt.get_Range(excel.Cells[rowIndex,colIndex],Excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//設置日期型的字段格式為居中對齊
}
else
if(col.DataType == System.Type.GetType("System.String"))
{
Excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();
xSt.get_Range(excel.Cells[rowIndex,colIndex],Excel.Cells[rowIndex,colIndex]).HorizontalAlignment = XlVAlign.xlVAlignCenter;//設置字符型的字段格式為居中對齊
}
else
{
Excel.Cells[rowIndex,colIndex] = row[col.ColumnName].ToString();
}
}
}
//
//加載一個合計行
//
int rowSum = rowIndex + 1;
int colSum = 2;
Excel.Cells[rowSum,2] = "合計";
xSt.get_Range(excel.Cells[rowSum,2],Excel.Cells[rowSum,2]).HorizontalAlignment = XlHAlign.xlHAlignCenter;
//
//設置選中的部分的顏色
//
xSt.get_Range(excel.Cells[rowSum,colSum],Excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[rowSum,colSum],Excel.Cells[rowSum,colIndex]).Interior.ColorIndex = 19;//設置為淺黃色,共計有56種
//
//取得整個報表的標題
//
Excel.Cells[2,2] = str;
//
//設置整個報表的標題格式
//
xSt.get_Range(excel.Cells[2,2],Excel.Cells[2,2]).Font.Bold = true;
xSt.get_Range(excel.Cells[2,2],Excel.Cells[2,2]).Font.Size = 22;
//
//設置報表表格為最適應寬度
//
xSt.get_Range(excel.Cells[4,2],Excel.Cells[rowSum,colIndex]).Select();
xSt.get_Range(excel.Cells[4,2],Excel.Cells[rowSum,colIndex]).Columns.AutoFit();
//
//設置整個報表的標題為跨列居中
//
xSt.get_Range(excel.Cells[2,2],Excel.Cells[2,colIndex]).Select();
xSt.get_Range(excel.Cells[2,2],Excel.Cells[2,colIndex]).HorizontalAlignment = XlHAlign.xlHAlignCenterAcrossSelection;
//
//繪制邊框
//
xSt.get_Range(excel.Cells[4,2],Excel.Cells[rowSum,colIndex]).Borders.LineStyle = 1;
xSt.get_Range(excel.Cells[4,2],Excel.Cells[rowSum,2]).Borders[XlBordersIndex.xlEdgeLeft].Weight = XlBorderWeight.xlThick;//設置左邊線加粗
xSt.get_Range(excel.Cells[4,2],Excel.Cells[4,colIndex]).Borders[XlBordersIndex.xlEdgeTop].Weight = XlBorderWeight.xlThick;//設置上邊線加粗
xSt.get_Range(excel.Cells[4,colIndex],Excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeRight].Weight = XlBorderWeight.xlThick;//設置右邊線加粗
xSt.get_Range(excel.Cells[rowSum,2],Excel.Cells[rowSum,colIndex]).Borders[XlBordersIndex.xlEdgeBottom].Weight = XlBorderWeight.xlThick;//設置下邊線加粗
//
//顯示效果
//
Excel.Visible=true;
//xSt.Export(Server.MapPath(".")+""+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHtml);
xBk.SaveCopyAs(Server.MapPath(".")+""+this.xlfile.Text+".xls");
ds = null;
xBk.Close(false, null,null);
Excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(Excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
Excel = null;
xSt = null;
GC.Collect();
string path = Server.MapPath(this.xlfile.Text+".xls");
System.IO.FileInfo file = new System.IO.FileInfo(path);
Response.Clear();
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
// 添加頭信息,為"文件下載/另存為"對話框指定默認文件名
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
// 添加頭信息,指定文件大小,讓浏覽器能夠顯示下載進度
Response.AddHeader("Content-Length", file.Length.ToString());
// 指定返回的是一個不能被客戶端讀取的流,必須被下載
Response.ContentType = "application/ms-Excel";
// 把文件流發送到客戶端
Response.WriteFile(file.FullName);
// 停止頁面的執行
Response.End();
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved