標籤:style blog http color os io ar for 資料
開發匯出excel,首先需要添加項目引用。
Microsoft.CSharp
這個是應用dynamic的前提。
在字碼頁,需要添加引用
using System.Runtime.InteropServices.Automation;
以下是我具體製作業務匯出的其中一個功能的代碼,供大家參考。
private ICommand _excelCommand; public ICommand ExcelCommand { get { if (_excelCommand == null) { _excelCommand = new RelayCommand<Grid>((g) => { dynamic excel = AutomationFactory.CreateObject("Excel.Application"); excel.Visible = true; dynamic workbook = excel.workbooks; workbook.Add(); dynamic sheet = excel.ActiveSheet; dynamic cell = null; int i = 1; // 將資料轉送到Excel foreach (BusinessBillOutModel item in SelectItems) { //ItemCollection.Add(item); //銷售日期 cell = sheet.Cells[i, 1]; // 列和行 cell.Value = item.BillDate; cell.ColumnWidth = 25; //名稱 cell = sheet.Cells[i, 2]; cell.Value = SettingSpace.CodeNameConvert(GlobalEnum.BaseType.BasePart, item.PartCode); //金額 cell = sheet.Cells[i, 3]; cell.Value = item.TotalPrice; //班組 cell = sheet.Cells[i, 4]; cell.Value = SettingSpace.CodeNameConvert(GlobalEnum.BaseType.TeamWork,item.TeamWork); //收款員(裡台員) cell = sheet.Cells[i, 5]; cell.Value = SettingSpace.CodeNameConvert(GlobalEnum.BaseType.UserType,item.SellPerson); //司機 cell = sheet.Cells[i, 6]; cell.Value = SettingSpace.CodeNameConvert(GlobalEnum.BaseType.UserType,item.AgentPerson); //備忘 cell = sheet.Cells[i, 6]; cell.Value = item.BillRemark; i++; } }); } return _excelCommand; } }View Code
//開發過程中遇到的問題。
在開發silverlight匯出excel時遇到【此操作在當前上下文中不受支援】,最後調試成功,具體原因,是在項目的屬性設定中沒有勾選一個選項,具體參看。
點擊 瀏覽器外設定,在彈出介面勾選 在瀏覽器之外運行時需要更高的信任。即可解決
silverlight匯出excel