第1篇:asp.net 將數(shù)據(jù)導出到excel中,,并設置其格式
-----------------------------------------------------------
添加引用:Microsoft Excel 11.0 Object Library ;
添加:using Microsoft.Office.Interop.Excel;
一、打開Excel文件============================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Open(@"E:/aaa.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
-
- excel1.Visible = true;
二,、新建Excel對象============================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(XlWBATemplate.xlWBATWorksheet或true);
-
- worksheet1.Activate();//激活sheet1表
-
- excel1.Visible = true;
三,、新建Excel表============================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(true);
-
- Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
-
- Worksheet worksheet1 =(Worksheet)workbook1.Worksheets.Add(Type.Missing,workbook1.Worksheets[1], 1, Type.Missing);
-
- excel1.Visible = true;
四、重命名Excel表名============================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(true);
-
- Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"或1];
-
- worksheet1.Name = "工作計劃表";
- excel1.Visible = true;
五,、設置或修改Excel表單元格內容========================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(true);
-
- Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
-
- worksheet1.Cells[1, 1] = "姓名";
-
- worksheet1.Cells[1, 2] = "性別";
-
- excel1.Visible = true;
六,、設置Excel表行寬和列高===========================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(true);
-
- Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
-
- worksheet1.Columns.ColumnWidth = 20;//全局行寬
-
- worksheet1.Columns.RowHeight = 20;//全局列高
-
- Range range1 = (Range) worksheet1.Cells[2, 1];
-
- range1.Columns.ColumnWidth = 40;//單元格行寬
-
- range1.Columns.RowHeight = 40;//單元格列高
-
- excel1.Visible = true;
七、設置Excel表單元格邊框===========================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(true);
-
- Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
-
- Range range1 = (Range)worksheet1.Cells[2, 2];
-
- range1.Borders.Color = System.Drawing.ColorTranslator.ToOle(Color.Red);
-
- range1.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlContinuous;
-
- range1.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlContinuous;
-
- range1.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous;
-
- range1.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous;
-
- //也可用后面的代碼代替上面四項range1.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic,null);
-
- range1.Borders.get_Item(XlBordersIndex.xlDiagonalDown).LineStyle = XlLineStyle.xlContinuous;//斜杠
-
- range1.Borders.get_Item(XlBordersIndex.xlDiagonalUp).LineStyle = XlLineStyle.xlContinuous;//反斜杠
-
- range1.Borders.get_Item(XlBordersIndex.xlDiagonalDown).Color = System.Drawing.ColorTranslator.ToOle(Color.Gold);
-
- excel1.Visible = true;
八,、Excel表塊操作============================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(true);
-
- Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
-
- Range range1 = worksheet1.get_Range("A2", "E8");//選擇操作塊
-
- range1.Font.Bold = true;//設置黑體
-
- range1.Font.Size = 18;//設置字體大小
-
- range1.Font.Name = "仿宋";//設置字體
-
- range1.Font.Color = System.Drawing.ColorTranslator.ToOle(Color.Blue);//設置字體顏色
-
- range1.HorizontalAlignment = XlHAlign.xlHAlignCenter;//設置水平對齊方式
-
- range1.VerticalAlignment = XlVAlign.xlVAlignCenter;//設置垂直對齊方式
-
- range1.Value2 = "123/r/n456";
-
- range1.Borders.get_Item(XlBordersIndex.xlEdgeTop).LineStyle = XlLineStyle.xlContinuous;
-
- range1.Borders.get_Item(XlBordersIndex.xlEdgeRight).LineStyle = XlLineStyle.xlContinuous;
-
- range1.Borders.get_Item(XlBordersIndex.xlEdgeBottom).LineStyle = XlLineStyle.xlContinuous;
-
- range1.Borders.get_Item(XlBordersIndex.xlEdgeLeft).LineStyle = XlLineStyle.xlContinuous;
-
- //也可用后面的代碼代替上面四項range1.BorderAround(XlLineStyle.xlContinuous, XlBorderWeight.xlThin, XlColorIndex.xlColorIndexAutomatic,null);
-
- range1.Borders.get_Item(XlBordersIndex.xlInsideHorizontal).LineStyle = XlLineStyle.xlContinuous;//塊內豎線
-
- range1.Borders.get_Item(XlBordersIndex.xlInsideVertical).LineStyle = XlLineStyle.xlContinuous;//塊內橫線
-
- excel1.Visible = true;
九,、Excel表單元格合并============================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(true);
-
- Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
-
- Range range1 = worksheet1.get_Range("A2", "E8");//選擇操作塊
-
- range1.Value2 = "123/r/n456";
-
- excel1.Application.DisplayAlerts = false;//使合并操作不提示警告信息
-
- range1.Merge(false);//參數(shù)為True則為每一行合并為一個單元格
-
- excel1.Application.DisplayAlerts = true;
-
- excel1.Visible = true;
十、復制Excel表============================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(true);
-
- Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
-
- worksheet1.Cells[1, 1] = "123";
-
- worksheet1.Copy(Type.Missing, worksheet1);
-
- Worksheet worksheet2 =(Worksheet)worksheet1.Next;
-
- //worksheet2.Name = "Sheet2";
-
- excel1.Visible = true;
十一,、頁面設置============================
- Microsoft.Office.Interop.Excel.Application excel1 = new Microsoft.Office.Interop.Excel.Application();
-
- Workbook workbook1 = excel1.Workbooks.Add(true);
-
- excel1.Caption = "我的報表";
-
- Worksheet worksheet1 = (Worksheet)workbook1.Worksheets["sheet1"];
-
- worksheet1.PageSetup.PaperSize = XlPaperSize.xlPaperA3;//紙張大小
-
- worksheet1.PageSetup.PrintTitleRows = "$1:$3";//頂端標題行
-
- worksheet1.PageSetup.Orientation = XlPageOrientation.xlLandscape;//頁面方向為橫向
-
- worksheet1.PageSetup.TopMargin = excel1.CentimetersToPoints(2);//上邊距為2厘米(厘米轉像素)
-
- worksheet1.PageSetup.BottomMargin = excel1.CentimetersToPoints(2);//下邊距為2厘米(厘米轉像素)
-
- worksheet1.PageSetup.LeftMargin = excel1.CentimetersToPoints(1.5);//左邊距為1.5厘米(厘米轉像素)
-
- worksheet1.PageSetup.RightMargin = excel1.CentimetersToPoints(1.5);//右邊距為1.5厘米(厘米轉像素)
-
- worksheet1.PageSetup.HeaderMargin = excel1.CentimetersToPoints(1.2);//頁眉邊距為1.2厘米(厘米轉像素)
-
- worksheet1.PageSetup.FooterMargin = excel1.CentimetersToPoints(1);//頁腳邊距為1厘米(厘米轉像素)
-
- worksheet1.PageSetup.CenterHorizontally = true;//頁面水平居中
-
- worksheet1.PageSetup.CenterVertically = false;//頁面不垂直居中
-
- worksheet1.PageSetup.CenterFooter = "第&P頁,,共&N頁";//中間頁腳內容
-
- excel1.Visible = true;
- function showMiniAd(){ var vn = "tinfo"; var dataURL = "http://t.sohu.com/third/user.jsp?passport="+window._xpt+"&vn="+vn; new LinkFile(dataURL, { type: 'script', noCache: false, callBack: { variable: vn, onLoad: function(){ var data = eval("(" + vn + ")") if(data != null && data.status == 1){ var userLink = data.url; //var userLink = data.icon; $('miniAd').innerHTML = "我正在玩搜狐微博,快來“關注”我,,了解我的最新動態(tài)吧,。
- "+userLink+""; $('miniAd').show(); } }, onFailure: function(){} }}); } showMiniAd();
轉自:http://sdshizq.blog.163.com/blog/static/109414056201052432126508/
第2篇: asp.net中將DataGrid中的數(shù)據(jù)導入到excel中,并設置其格式
----------------------------------------------------------------------------
終于完成了從datagrid 中導入excel,,為了防止忘記,,特意記錄下來,為大家和自己提供方便,。
web應用程序中主要代碼如下:
- //設置DataGrid2數(shù)據(jù)源 ,,并綁定(由于這一步很簡單,,所以略過)
-
- /**//*設置DataGrid2的格式為文本型,這樣就解決了導入excel之后,,形如“00000123”變成了“123”的問題,。在這里,為了簡單起見,,我設置了dataGrid總的屬性,。也可以為每個單元格設置屬性,如DataGrid2.Items[0].Cells[0].Attributes.Add("style","vnd.ms-excel.numberformat:@");*/
- DataGrid2.Attributes.Add("style","vnd.ms-excel.numberformat:@");
-
- //將DataGrid2中的數(shù)據(jù)以劉的形式寫入excel文件中
- Response.Clear();
- Response.Buffer= true;
- Response.Charset="GB2312";
- Response.AppendHeader("Content-Disposition","attachment;filename=zjxx.xls");
- Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//設置輸出流為簡體中文
- Response.ContentType = "application/ms-excel";//設置輸出文件類型為excel文件,。
- this.EnableViewState = false;
- System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
- System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
- System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
- this.DataGrid2.RenderControl(oHtmlTextWriter);
- Response.Write(oStringWriter.ToString());
- Response.End();
windows應用程序中如下:
- //其中zjtable中已經從數(shù)據(jù)庫中讀入了數(shù)據(jù)
- Excel.ApplicationClass excelApp ;
- excelApp = new Excel.ApplicationClass();
- Excel.Workbook excelBook =excelApp.Workbooks.Add(1);
- Excel.Worksheet excelSheet=(Excel.Worksheet)excelBook.Worksheets[1];
-
- excelApp.Visible=true;
- excelSheet.Cells[1,1]="姓名";
- excelSheet.Cells[1,2]="性別";
- excelSheet.Cells[1,3]="出生日期";
-
- //設置excel文件中所有的單元格為文本型
- excelSheet.Cells.NumberFormatLocal="@";
- for(int i=0;i < zjtable.Rows.Count ;i++)
- ...{
- //將zjtable中的數(shù)據(jù)導入到excel文件中
- DataRow row=zjtable.Rows[i];
- for (int j=1;j<=3;j++)
- excelSheet.Cells[i+2,j]=row[j].ToString();
- }
本文來自CSDN博客,,出處:http://blog.csdn.net/tthxqttzln/archive/2006/09/18/1240044.aspx
第3篇: asp.net中將DataTable中的數(shù)據(jù)導出到excel中,并設置其格式【中間件】
----------------------------------------------------------------------------
中間包xml格式 下面是一個ExcelExporter.cs檔,,我用一個物件去參考它後,,用裡面的Export(DataSet, ExcelFileName)就可以跳出另存新檔的視窗! 就可以順利存成Excel檔了,!
我要如何像之前的文章中所做的一樣,,像加下面這一行,就可以在Excel中正常顯示,! 依照我提供的Code要加在哪,??我試好久摟,,東塞西塞也沒用,!>,<"... this.page.Response.Write( "<style>Data { mso-number-format:/"http://@/"; } </style>" ); 這一段直接全部Copy就可以用了,!
第4篇:asp.net用模板導出excel的類
-----------------------------------
- <3>用模板列導出excel類
- 前臺設置
- <asp:Button runat="server" ID="btn_Excel2" CausesValidation="False" Text="導Excel" UseSubmitBehavior="False" CssClass="button" OnClick="btn_Excel_Click" />
- <asp:PostBackTrigger ControlID="btn_Excel2" />
- 後臺類
- //public override void VerifyRenderingInServerForm(Control control)
- //{
- //}
- public void OutputExcel(DataView dv, string str)
- {
- #region
- GC.Collect();
-
- //ExcelOperate excelOperate = new ExcelOperate();
- Application excel;// = new Application();
- //int rowIndex = 4;
- //int colIndex = 1;
-
- _Workbook xBk;
- _Worksheet xSt;
-
- excel = new ApplicationClass();
-
- xBk = excel.Workbooks.Add(Server.MapPath("xls_template") + "http://標準工時模板.xls");
-
- xSt = (_Worksheet)xBk.ActiveSheet;
-
- xSt.get_Range(xSt.Cells[2, 1], xSt.Cells[2, 9]).Merge(Missing.Value); //橫向合并
- xSt.get_Range(xSt.Cells[2, 1], xSt.Cells[2, 1]).Value2 = GetDept(Session["sa_kb"].ToString()) + " " + "標準工時";
- //13838
- xSt.Cells[3, 1] = "成品料號:" + Session["sa001"].ToString();
- xSt.Cells[3, 3] = "型號:" + Session["sa02x"].ToString();
- xSt.Cells[3, 8] = "版次/版本:" + Session["banchi"].ToString();
- xSt.Cells[4, 1] = "半成品料號:" + Session["sa011"].ToString();
- xSt.Cells[4, 3] = "訂單:" + Session["sa_NO"].ToString();
- xSt.Cells[4, 8] = "IE:" + Session["sa_IE"].ToString();
- for (int i = 0; i < dv.Count; i++)
- {
- xSt.Cells[7 + i, 1] = dv[i].Row[0].ToString();
-
- xSt.Cells[7 + i, 2] = dv[i].Row[1].ToString();
- xSt.Cells[7 + i, 3] = dv[i].Row[3].ToString();
- xSt.Cells[7 + i, 4] = dv[i].Row[4].ToString();
- xSt.Cells[7 + i, 5] = dv[i].Row[5].ToString();
-
- if (dv[i].Row[2].ToString() == "1")//若是有數(shù)據(jù)變更的則著色表示
- {
-
- SetBold(xSt, xSt.Cells[7 + i, 1], xSt.Cells[7 + i, 9], 1);
-
- }
-
-
- SetHAlignCenter(xSt, xSt.Cells[7 + i, 6], xSt.Cells[7 + i, 9]);//居中
-
- }
- int rowNum = 7;
- DataView dvw = GetBiao();
- for (int i = 0; i < dvw.Count; i++)
- {
- int rowAdd = Convert.ToInt32(dvw[i].Row[1].ToString());
-
- xSt.get_Range(xSt.Cells[rowNum, 6], xSt.Cells[rowNum + rowAdd - 1, 6]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[rowNum, 6], xSt.Cells[rowNum + rowAdd - 1, 6]).Value2 = dvw[i].Row[2].ToString();
- xSt.get_Range(xSt.Cells[rowNum, 7], xSt.Cells[rowNum + rowAdd - 1, 7]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[rowNum, 7], xSt.Cells[rowNum + rowAdd - 1, 7]).Value2 = dvw[i].Row[3].ToString();
- xSt.get_Range(xSt.Cells[rowNum, 8], xSt.Cells[rowNum + rowAdd - 1, 8]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[rowNum, 8], xSt.Cells[rowNum + rowAdd - 1, 8]).Value2 = dvw[i].Row[4].ToString();
- xSt.get_Range(xSt.Cells[rowNum, 9], xSt.Cells[rowNum + rowAdd - 1, 9]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[rowNum, 9], xSt.Cells[rowNum + rowAdd - 1, 9]).Value2 = dvw[i].Row[6].ToString();
- rowNum = rowNum + rowAdd;
-
- }
-
- xSt.get_Range(xSt.Cells[(int)dv.Count + 7, 1], xSt.Cells[(int)dv.Count + 7, 2]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[(int)dv.Count + 7, 1], xSt.Cells[(int)dv.Count + 7, 2]).Value2 = "間接人員";
- #region
- int dvc = (int)dv.Count + 7;
- xSt.Cells[dvc + 1, 1] = "1";
- xSt.Cells[dvc + 2, 1] = "2";
- xSt.Cells[dvc + 3, 1] = "3";
- xSt.Cells[dvc + 1, 2] = "修改";
- xSt.Cells[dvc + 2, 2] = "調機";
- xSt.Cells[dvc + 3, 2] = "備注";
- string[] str1 = GetSum("1").Split(';');
- xSt.Cells[dvc + 1, 3] = str1[0];
- xSt.Cells[dvc + 1, 4] = str1[1];
- xSt.Cells[dvc + 1, 5] = str1[2];
- xSt.Cells[dvc + 1, 6] = str1[3];
- string[] str2 = GetSum("2").Split(';');
- xSt.Cells[dvc + 2, 3] = str2[0];
- xSt.Cells[dvc + 2, 4] = str2[1];
- xSt.Cells[dvc + 2, 5] = str2[2];
- xSt.Cells[dvc + 2, 6] = str2[3];
- string[] str3 = GetSum("3").Split(';');
- xSt.Cells[dvc + 3, 3] = str3[0];
- xSt.Cells[dvc + 3, 4] = str3[1];
- xSt.Cells[dvc + 3, 5] = str3[2];
- xSt.Cells[dvc + 3, 6] = str3[3];
- #endregion
-
- int ExRow = (int)dv.Count + 12;
- // 將圖片寫入到Excel
- xSt.get_Range(xSt.Cells[ExRow, 6], xSt.Cells[ExRow + 6, 9]).Merge(Missing.Value);
- Worksheet wk = (Worksheet)xSt;
- int heit = ((int)dv.Count) * 22;
- heit = heit - 30;
-
- string strpath = Session["sa010"].ToString();
- if (strpath != "")
- {
- wk.Shapes.AddPicture(strpath, MsoTriState.msoFalse, MsoTriState.msoTrue, 455, heit, 113, 84);
- }
- xSt.get_Range(xSt.Cells[ExRow, 6], xSt.Cells[ExRow + 6, 9]).Value2 = "";
- xSt.Cells[ExRow, 2] = "產線總人數(shù)(人):";
- xSt.Cells[ExRow + 1, 2] = "產線瓶頸時間(秒):";
- xSt.Cells[ExRow + 2, 2] = "產線平衡率(%):";
- xSt.Cells[ExRow + 3, 2] = "產品標準工時(秒):";
- xSt.Cells[ExRow + 4, 2] = "標準日產能(PCS/10H):";
- //設置位置
- SetHAlignCenter(xSt, xSt.Cells[ExRow, 2], xSt.Cells[ExRow, 2], "str");
- SetHAlignCenter(xSt, xSt.Cells[ExRow + 1, 2], xSt.Cells[ExRow + 1, 2], "str");
- SetHAlignCenter(xSt, xSt.Cells[ExRow + 2, 2], xSt.Cells[ExRow + 2, 2], "str");
- SetHAlignCenter(xSt, xSt.Cells[ExRow + 3, 2], xSt.Cells[ExRow + 3, 2], "str");
- SetHAlignCenter(xSt, xSt.Cells[ExRow + 4, 2], xSt.Cells[ExRow + 4, 2], "str");
- //設置字體產色
-
- #region
- SetBold(xSt, xSt.Cells[ExRow, 2], xSt.Cells[ExRow, 2], "");
- SetBold(xSt, xSt.Cells[ExRow + 1, 2], xSt.Cells[ExRow + 1, 2], "");
- SetBold(xSt, xSt.Cells[ExRow + 2, 2], xSt.Cells[ExRow + 2, 2], "");
- SetBold(xSt, xSt.Cells[ExRow + 3, 2], xSt.Cells[ExRow + 3, 2], "");
- SetBold(xSt, xSt.Cells[ExRow + 4, 2], xSt.Cells[ExRow + 4, 2], "");
- #endregion
-
- xSt.get_Range(xSt.Cells[ExRow, 3], xSt.Cells[ExRow, 5]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[ExRow, 3], xSt.Cells[ExRow, 5]).Value2 = GetSum("4");
- SetBold(xSt, xSt.Cells[ExRow, 3], xSt.Cells[ExRow, 5], "");
- xSt.get_Range(xSt.Cells[ExRow + 1, 3], xSt.Cells[ExRow + 1, 5]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[ExRow + 1, 3], xSt.Cells[ExRow + 1, 5]).Value2 = GetSum("5");
- SetBold(xSt, xSt.Cells[ExRow + 1, 3], xSt.Cells[ExRow + 1, 5], "");
- xSt.get_Range(xSt.Cells[ExRow + 2, 3], xSt.Cells[ExRow + 2, 5]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[ExRow + 2, 3], xSt.Cells[ExRow + 2, 5]).Value2 = GetSum("6");
- SetBold(xSt, xSt.Cells[ExRow + 2, 3], xSt.Cells[ExRow + 2, 5], "");
-
- xSt.get_Range(xSt.Cells[ExRow + 3, 3], xSt.Cells[ExRow + 3, 5]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[ExRow + 3, 3], xSt.Cells[ExRow + 3, 5]).Value2 = GetSum("7");
- SetBold(xSt, xSt.Cells[ExRow + 3, 3], xSt.Cells[ExRow + 3, 5], "");
- xSt.get_Range(xSt.Cells[ExRow + 4, 3], xSt.Cells[ExRow + 4, 5]).Merge(Missing.Value);
- xSt.get_Range(xSt.Cells[ExRow + 4, 3], xSt.Cells[ExRow + 4, 5]).Value2 = GetSum("8");
- SetBold(xSt, xSt.Cells[ExRow + 4, 3], xSt.Cells[ExRow + 4, 5], "");
- //居中
- SetHAlignCenter(xSt, xSt.Cells[ExRow, 3], xSt.Cells[ExRow, 5]);
- SetHAlignCenter(xSt, xSt.Cells[ExRow + 1, 3], xSt.Cells[ExRow + 1, 5]);
- SetHAlignCenter(xSt, xSt.Cells[ExRow + 2, 3], xSt.Cells[ExRow + 2, 5]);
- SetHAlignCenter(xSt, xSt.Cells[ExRow + 3, 3], xSt.Cells[ExRow + 3, 5]);
- SetHAlignCenter(xSt, xSt.Cells[ExRow + 4, 3], xSt.Cells[ExRow + 4, 5]);
-
- xSt.get_Range(xSt.Cells[ExRow + 5, 1], xSt.Cells[ExRow + 5, 5]).Merge(Missing.Value); //橫向合并
- xSt.get_Range(xSt.Cells[ExRow + 5, 1], xSt.Cells[ExRow + 5, 5]).Value2 = "注: 適用型號: ";
-
- //
- //顯示效果
- //
- excel.Visible = false;
-
- //xSt.Export(Server.MapPath(".")+"http://"+this.xlfile.Text+".xls",SheetExportActionEnum.ssExportActionNone,Microsoft.Office.Interop.OWC.SheetExportFormat.ssExportHTML);
-
- string stick = DateTime.Now.Ticks.ToString();
- xBk.SaveCopyAs(Server.MapPath("xls_files") + "http://" + stick + ".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("xls_files") + "http://" + stick + ".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);
- // 停止頁面的執(zhí)行
-
- Response.End();
- #endregion
- }
-
|