使用ExcelPlate,如何不要透過Excel即可直接印表?
可以在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時, 出現”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");來用默認的程式打開文件。
如何用程式實現word查找替換方法,現有方法出現”OfficeTools字串參數太長”錯誤訊息?
查找替換功能有字元數的限制,替換的最大長度為255個字元,應該直接修改Word中的Sentences或者Words對象來實現,如:
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
StreamReader reader = new StreamReader(dialog.FileName, Encoding.UTF8);
string text = reader.ReadToEnd();
reader.Close();
Word.Application objWord = new Word.Application();
if (objWord == null)
{
throw new Exception("Word could not be started. Check that your office installation and project references are correct");
}
objWord.Visible = true;
objWord.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
object objMiss = Missing.Value;
Word.Document objDocument = objWord.Documents.Add(ref objMiss, ref objMiss, ref objMiss, ref objMiss);
try
{
objDocument.Sentences[1].Text = text;
object filename = dialog.FileName + ".doc";
objDocument.SaveAs(ref filename, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
}
finally
{
((Word._Application)objWord).Quit(ref objMiss, ref objMiss, ref objMiss);
Marshal.ReleaseComObject(objDocument);
Marshal.ReleaseComObject(objWord);
objDocument = null;
objWord = null;
objMiss = null;
GC.Collect();
}
}