C#將不規(guī)則表格數(shù)據(jù)導(dǎo)出到Excel文件的代碼

學(xué)習(xí)閑暇時間,將做工程過程較好的一些內(nèi)容做個收藏,下面內(nèi)容是關(guān)于C#將不規(guī)則表格數(shù)據(jù)導(dǎo)出到Excel文件的內(nèi)容,應(yīng)該是對小伙伴們有所用。

public void OutputExcel(DataView dv,string str)

{

? GC.Collect();

? 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;

? }

? 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");

? ? }

? ? else

? ? ? if(col.DataType == System.Type.GetType("System.String"))

? ? {

? ? ? excel.Cells[rowIndex,colIndex] = "'"+row[col.ColumnName].ToString();

? ? }

? ? 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();

? 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;

? excel.Visible=true;

? 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();

}

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

上面的方面,均將要導(dǎo)出的execl數(shù)據(jù),直接給瀏覽器輸出文件流,下面的方法是首先將其存到服務(wù)器的某個文件夾中,然后把文件發(fā)送到客戶端。這樣可以持久的把導(dǎo)出的文件存起來,以便實現(xiàn)其它功能。

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ?

? ? ? ? ? ? ? ?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容