|
使用ExcelPlate每一次列印至 Excel時, 出現”output.xls has already exsited.Would you like to overwrite it”,應如何解決? |
觀看回應
|
|
這是防止在輸出過程中,會覆蓋原來存在的Excel檔案所做的警告,如果檔案不存在的話是不會彈出警告的,所以可以在ExcelPlate.Output()前刪除這個檔案,如下: Using System.IO; string filepath = string.Empty; if (excelPlate1.OutputPath.EndsWith("\\")) { filepath = excelPlate1.OutputPath + excelPlate1.OutputFileName; } else { filepath = excelPlate1.OutputPath + "\\" + excelPlate1.OutputFileName; } if (File.Exists(filepath)) { try { File.Delete(filepath); } catch { } }
|
|
|
|
如何對ExcelPlate中某一行設定格式? |
觀看回應
|
|
ExcelPlate 的格式是要求先設定的,所以對於這種需求,需要另外寫代碼來實現. 如:對A1單元格畫底線,範例代碼如下: 需要添加Excel.dll引用,可以在OfficeTools下找到 using System.Reflection; using System.Runtime.InteropServices; string file = @"c:\test.xls";//xls的檔案名 Excel.Application application = new Excel.Application();//打開Excel進程 Excel.Workbook workBook = null; Excel.Worksheet workSheet = null; Excel.Range cell = null; try { application.Visible = false;//不顯示Excel程式 application.DisplayAlerts = false;//不顯示確認窗口 workBook = application.Workbooks.Open(file, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);//打開文件 workSheet = workBook.Worksheets[1] as Excel.Worksheet;//取得第一個WorkSheet cell = workSheet.get_Range("A1", "A1");//取得A1這個單元格 cell.Font.Underline = true;//設置下劃線 workBook.Save();//保存檔 } finally { application.Quit();//退出Excel進程 Marshal.ReleaseComObject(application);//釋放Excel進程 Marshal.ReleaseComObject(workBook);//釋放Excel進程 Marshal.ReleaseComObject(workSheet);//釋放Excel進程 Marshal.ReleaseComObject(cell);//釋放Excel進程 application = null; workBook = null; workSheet = null; cell = null; GC.Collect();//回收 }
|
|
|
|
如何在ExcelPlate的AfterOutput事件中加入列印的代碼? |
觀看回應
|
|
using System.Reflection; using System.Runtime.InteropServices; private void excelPlate1_AfterOutput(object sender, EventArgs e) { Excel.Application objExcel = new Excel.Application(); objExcel.Visible= false; Excel.Workbook objWorkBook = objExcel.Workbooks.Open(excelPlate1.FilePath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); Try { objWorkBook.PrintOut(1, 1, 1, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value); } Finally { objExcel.Quit(); Marshal.ReleaseComObject(objExcel); Marshal.ReleaseComObject(objWorkBook); objExcel = null; objWorkBook = null; GC.Collect(); } } PrintOut為列印的函數,第一個參數是列印起始頁,第二個參數列印結束頁,第三個參數是列印的份數。
|
|
|
|
請問如何下載ExcelPlate產生出虛擬目錄之Excel檔案? |
觀看回應
|
|
可以在Button_Click事件中添加如下代碼: FileInfo DownloadFile = new FileInfo("C:\\1.xls");//這裏換成excel檔的完整路徑,可以用Page.Server.MapPath(WebExcelPlate.FilePath)來取得 Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.ContentType = "application/octet-stream";//attachment/online Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8).Replace('+', ' ')); Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); Response.WriteFile(DownloadFile.FullName); Response.Flush(); Response.End();
|
|
|
|
用什麼樣的方法可以讓檔下載後自動打開? |
觀看回應
|
|
1. WebForm COM組件中加入Microsoft.Office.Interop.Excel,讓Excel.Application 可以使用. Excel.Application Myexcel=new Excel.Application(); Myexcel.Workbooks.Open(filename, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing); 2. WinForm 下載後WinForm可以通過 System.Diagnostics.Process.Start方法,如: System.Diagnostics.Process.Start(@"C:\test.txt");來用默認的程式打開文件。
|
|
|