public static void Encode(Workbook workbook, Stream stream) { List<Record> records = EncodeWorkbook(workbook); BinaryWriter writer = new BinaryWriter(stream); foreach (Record record in records) { record.Write(writer); } writer.Close(); }
public static Workbook Decode(Stream stream) { Workbook book = new Workbook(); SharedResource sharedResource; List<Record> records = ReadRecords(stream, out book.DrawingGroup); book.Records = records; List<BOUNDSHEET> boundSheets = DecodeRecords(records, out sharedResource); foreach (BOUNDSHEET boundSheet in boundSheets) { stream.Position = boundSheet.StreamPosition; Worksheet sheet = WorksheetDecoder.Decode(book, stream, sharedResource); sheet.Book = book; sheet.Name = boundSheet.SheetName; sheet.SheetType = (SheetType)boundSheet.SheetType; book.Worksheets.Add(sheet); } return book; }
private void OnExportCommand() { SaveFileDialog sDialog = new SaveFileDialog(); sDialog.Filter = "Excel Files(*.xls)|*.xls"; if (sDialog.ShowDialog() == true) { try { Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet("产品汇总"); //Title worksheet.Cells[0, 0] = new Cell("生产令号"); worksheet.Cells[0, 1] = new Cell("项目名称"); worksheet.Cells[0, 2] = new Cell("产品序号"); worksheet.Cells[0, 3] = new Cell("产品名称"); worksheet.Cells[0, 4] = new Cell("出厂编号"); Int16 RowCount = 1; foreach (ProductEntity productEntity in ProductEntityList) { int columnCount = 0; if (ProjectNameSearch != null && ProjectNameSearch.Length > 0) { if (!productEntity.ProjectName.Contains(ProjectNameSearch)) { continue; } } worksheet.Cells[RowCount, columnCount++] = new Cell(productEntity.ManufactureNumber); worksheet.Cells[RowCount, columnCount++] = new Cell(productEntity.ProjectName); worksheet.Cells[RowCount, columnCount++] = new Cell(productEntity.ProductID); worksheet.Cells[RowCount, columnCount++] = new Cell(productEntity.ProductName); worksheet.Cells[RowCount, columnCount++] = new Cell(productEntity.ProductOutputNumber); RowCount++; } workbook.Worksheets.Add(worksheet); Stream sFile = sDialog.OpenFile(); workbook.Save(sFile); ProductManager.Controls.Message.InfoMessage("导出成功"); } catch (Exception outputE) { string errorMessage = "导出文件失败:" + outputE.Message; ProductManager.Controls.Message.ErrorMessage(errorMessage); } } }
private void ExportToExcelBtn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { SaveFileDialog sDialog = new SaveFileDialog(); sDialog.Filter = "Excel Files(*.xls)|*.xls"; if (sDialog.ShowDialog() == true) { WriteableBitmap bitmap = new WriteableBitmap(1000, 500); bitmap.Render(MainChart, null); bitmap.Invalidate(); Workbook workbook = new Workbook(); Worksheet worksheet1 = new Worksheet("SheetWithImage"); Lite.ExcelLibrary.SpreadSheet.Picture pic = new Lite.ExcelLibrary.SpreadSheet.Picture(); System.Windows.Controls.Image image = new System.Windows.Controls.Image(); image.Source = bitmap; image.Name = "imgExport"; image.Width = 1000; image.Height = 500; image.Stretch = Stretch.Fill; pic.Image = new Lite.ExcelLibrary.SpreadSheet.Image(ImageTranslator.TranslateImageToBytes(image), 0xF01E); pic.TopLeftCorner = new CellAnchor(1, 1, 10, 10); pic.BottomRightCorner = new CellAnchor(24, 16, 10, 10); worksheet1.AddPicture(pic); workbook.Worksheets.Add(worksheet1); Stream sFile = sDialog.OpenFile(); workbook.Save(sFile); } }
private static List<Record> EncodeWorkbook(Workbook workbook) { SharedResource sharedResource = new SharedResource(true); List<Record> book_records = new List<Record>(); BOF bof = new BOF(); bof.BIFFversion = 0x0600; //0600H = BIFF8 bof.StreamType = StreamType.WorkbookGlobals; bof.BuildID = 3515; bof.BuildYear = 1996; bof.RequiredExcelVersion = 6; book_records.Add(bof); CODEPAGE codepage = new CODEPAGE(); //codepage.CodePageIdentifier = (ushort)Encoding.Unicode.CodePage; codepage.CodePageIdentifier = (ushort)1200; book_records.Add(codepage); WINDOW1 window = new WINDOW1(); window.WindowWidth = 16384; window.WindowHeight = 8192; window.SelecteWorksheets = 1; window.TabBarWidth = 600; window.OptionFlags = 56; book_records.Add(window); DATEMODE dateMode = new DATEMODE(); dateMode.Mode = 1; sharedResource.BaseDate = DateTime.Parse("1904-01-01"); book_records.Add(dateMode); List<List<Record>> all_sheet_records = new List<List<Record>>(); foreach (Worksheet worksheet in workbook.Worksheets) { List<Record> sheet_records = WorkSheetEncoder.Encode(worksheet, sharedResource); Record.EncodeRecords(sheet_records); all_sheet_records.Add(sheet_records); } book_records.AddRange(sharedResource.FormatRecords.ToArray()); book_records.AddRange(sharedResource.ExtendedFormats.ToArray()); List<BOUNDSHEET> boundSheets = new List<BOUNDSHEET>(); foreach (Worksheet worksheet in workbook.Worksheets) { BOUNDSHEET boundSheet = new BOUNDSHEET(); boundSheet.Visibility = 0; // 00H = Visible boundSheet.SheetType = (byte)SheetType.Worksheet; boundSheet.SheetName = worksheet.Name; boundSheet.StreamPosition = 0; boundSheets.Add(boundSheet); book_records.Add(boundSheet); } if (sharedResource.Images.Count > 0) { book_records.Add(EncodeImages(sharedResource.Images)); } Record.EncodeRecords(book_records); int sstOffset = Record.CountDataLength(book_records); book_records.Add(sharedResource.SharedStringTable); book_records.Add(CreateEXTSST(sharedResource.SharedStringTable, sstOffset)); EOF eof = new EOF(); book_records.Add(eof); Record.EncodeRecords(book_records); int dataLength = Record.CountDataLength(book_records); for (int i = 0; i < workbook.Worksheets.Count; i++) { boundSheets[i].StreamPosition = (uint)dataLength; boundSheets[i].Encode(); int sheet_length = Record.CountDataLength(all_sheet_records[i]); dataLength += sheet_length; } List<Record> all_records = new List<Record>(); all_records.AddRange(book_records); foreach (List<Record> sheet_records in all_sheet_records) { all_records.AddRange(sheet_records); } return all_records; }
void PlanListWindow_Closed(object sender, EventArgs e) { PlanListTraceWindow planListWindow = sender as PlanListTraceWindow; if (planListWindow.DialogResult == true) { SaveFileDialog sDialog = new SaveFileDialog(); sDialog.Filter = "Excel Files(*.xls)|*.xls"; if (sDialog.ShowDialog() == true) { try { string versionId = "文件编号:"; if (null != planListWindow.planExtraEntity && null != planListWindow.planExtraEntity.FileId) { versionId += planListWindow.planExtraEntity.FileId; } versionId += " 计划版本:"; if (null != planListWindow.planExtraEntity && null != planListWindow.planExtraEntity.VersionId) { versionId += planListWindow.planExtraEntity.VersionId; } string projectNameKey = null == planListWindow.planExtraEntity ? "所有项目" : null != SelectProjectEntity ? SelectProjectEntity.ProjectName : planListWindow.planListViewModelList[0].PlanList[0].ProjectName; projectNameKey += " "; string manufactureNumber = "生产令号:"; if (null != planListWindow.planExtraEntity && null != planListWindow.planExtraEntity.ManufactureNumber) { manufactureNumber += planListWindow.planExtraEntity.ManufactureNumber; } ColumnModel columnModel = new ColumnModel(); Dictionary<string, Worksheet> sheetDictionary = new Dictionary<string, Worksheet>(); Dictionary<string, int> rowCountDictionary = new Dictionary<string, int>(); Worksheet currentSheet = null; foreach(PlanListViewModel model in planListWindow.planListViewModelList) { foreach (PlanEntity planEntity in model.FilterPlanList) { if ("设计完成节点" == planEntity.SheetName) { planEntity.SheetName = "设计节点"; } else if ("采购完成节点" == planEntity.SheetName) { planEntity.SheetName = "采购节点"; } else if ("生产完成节点" == planEntity.SheetName) { planEntity.SheetName = "生产节点"; } if (!sheetDictionary.Keys.Contains(planEntity.SheetName)) { currentSheet = new Worksheet(planEntity.SheetName); string projectNameName = projectNameKey + planEntity.SheetName; Int16 rowHeaderCount = 0; currentSheet.Cells[rowHeaderCount++, 0] = new Cell(versionId); currentSheet.Cells[rowHeaderCount++, 0] = new Cell(projectNameName); currentSheet.Cells[rowHeaderCount++, 0] = new Cell(manufactureNumber); int columnHeaderCount = 0; if (null == planListWindow.planExtraEntity) { currentSheet.Cells[rowHeaderCount, columnHeaderCount++] = new Cell("项目名称"); currentSheet.Cells[rowHeaderCount, columnHeaderCount++] = new Cell("生产令号"); currentSheet.Cells[rowHeaderCount, columnHeaderCount++] = new Cell("版本号"); } int columnModelIndex = model.ColumnModelIndex; foreach (string itemColumn in columnModel.List[columnModelIndex]) { currentSheet.Cells[rowHeaderCount, columnHeaderCount++] = new Cell(itemColumn); } ++rowHeaderCount; sheetDictionary.Add(planEntity.SheetName, currentSheet); rowCountDictionary.Add(planEntity.SheetName, rowHeaderCount); } else { currentSheet = sheetDictionary[planEntity.SheetName]; } int rowCount = rowCountDictionary[planEntity.SheetName]; int columnCount = 0; if (null == planListWindow.planExtraEntity) { currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.ProjectName); currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.ManufactureNumber); currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.VersionId); } string value = Convert.ToString(planEntity.SequenceId); currentSheet.Cells[rowCount, columnCount++] = new Cell(value); currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.ComponentName); currentSheet.Cells[rowCount, columnCount++] = new Cell(planEntity.TaskDescription); value = Convert.ToString(planEntity.Weight); currentSheet.Cells[rowCount, columnCount++] = new Cell(value); if (planEntity.Score.HasValue) { value = Convert.ToString(planEntity.Score.Value); currentSheet.Cells[rowCount, columnCount] = new Cell(value); } ++columnCount; int currentColumnModelIndex = model.ColumnModelIndex; if (1 == currentColumnModelIndex) { if (planEntity.OrderDate.HasValue) { value = planEntity.OrderDate.Value.ToString(String.Format("yyyy-MM-dd")); currentSheet.Cells[rowCount, columnCount] = new Cell(value); } ++columnCount; } value = planEntity.TargetDate.ToString(String.Format("yyyy-MM-dd")); currentSheet.Cells[rowCount, columnCount++] = new Cell(value); if (planEntity.TargetDateAdjustment1.HasValue) { value = planEntity.TargetDateAdjustment1.Value.ToString(String.Format("yyyy-MM-dd")); currentSheet.Cells[rowCount, columnCount] = new Cell(value); } ++columnCount; if (planEntity.TargetDateAdjustment2.HasValue) { value = planEntity.TargetDateAdjustment2.Value.ToString(String.Format("yyyy-MM-dd")); currentSheet.Cells[rowCount, columnCount] = new Cell(value); } ++columnCount; if (planEntity.AccomplishDate.HasValue) { value = planEntity.AccomplishDate.Value.ToString(String.Format("yyyy-MM-dd")); currentSheet.Cells[rowCount, columnCount] = new Cell(value); } ++columnCount; if (null != planEntity.DepartmentName && string.Empty != planEntity.DepartmentName) { currentSheet.Cells[rowCount, columnCount] = new Cell(planEntity.DepartmentName); } ++columnCount; if (null != planEntity.Remark && string.Empty != planEntity.Remark) { currentSheet.Cells[rowCount, columnCount] = new Cell(planEntity.Remark); } ++columnCount; ++rowCountDictionary[planEntity.SheetName]; } } if(sheetDictionary.Count > 0) { Workbook workbook = new Workbook(); foreach (KeyValuePair<string, Worksheet> kv in sheetDictionary) { if (null != planListWindow.planExtraEntity) { PlanExtraEntity planExtraEntity = planListWindow.planExtraEntity; currentSheet = kv.Value; currentSheet.Cells[rowCountDictionary[kv.Key]++, 0] = new Cell("编制依据:" + planExtraEntity.CompilationBasis); currentSheet.Cells[rowCountDictionary[kv.Key]++, 0] = new Cell("第一次调整原因:" + planExtraEntity.ReasonAdjustment1); currentSheet.Cells[rowCountDictionary[kv.Key]++, 0] = new Cell("第二次调整原因:" + planExtraEntity.ReasonAdjustment2); currentSheet.Cells[rowCountDictionary[kv.Key], 0] = new Cell("编制:" + planExtraEntity.CompileUserName); currentSheet.Cells[rowCountDictionary[kv.Key], 3] = new Cell("审核:" + planExtraEntity.ExamineUserName); currentSheet.Cells[rowCountDictionary[kv.Key]++, 5] = new Cell("批准:" + planExtraEntity.ApproveUserName); currentSheet.Cells[rowCountDictionary[kv.Key], 0] = new Cell("日期:" + planExtraEntity.CompileDate); currentSheet.Cells[rowCountDictionary[kv.Key], 3] = new Cell("日期:" + planExtraEntity.ExamineDate); currentSheet.Cells[rowCountDictionary[kv.Key]++, 5] = new Cell("日期:" + planExtraEntity.ApproveDate); } workbook.Worksheets.Add(kv.Value); } Stream sFile = sDialog.OpenFile(); workbook.Save(sFile); Message.InfoMessage("导出成功"); } else { Message.ErrorMessage("导出失败:无数据!"); } } catch (Exception outputE) { string errorMessage = "导出文件失败:" + outputE.Message; Message.ErrorMessage(errorMessage); } } } if (planListWindow.DialogResult.HasValue) { ObservableCollection<string> unFinishedProject = new ObservableCollection<string>(); foreach (PlanListViewModel planListViewModel in planListWindow.planListViewModelList) { foreach (PlanEntity planEntity in planListViewModel.PlanList) { if (null == planEntity.AccomplishDate && !unFinishedProject.Contains(planEntity.ManufactureNumber)) { unFinishedProject.Add(planEntity.ManufactureNumber); } } } if (null != planListWindow.planExtraEntity) { if(null != SelectProjectEntity) { SelectProjectEntity.AccomplishMark = (0 == unFinishedProject.Count ? 1 : 0); SelectProjectEntity.DUpdate(); } } else { foreach (ProjectEntity projectItem in ProjectList) { if (!string.IsNullOrEmpty(projectItem.PlanVersionID)) { projectItem.AccomplishMark = unFinishedProject.Contains(projectItem.ManufactureNumber) ? 0 : 1; projectItem.DUpdate(); } } } SaveChanges(); } (OnViewSinglePlan as DelegateCommand).RaiseCanExecuteChanged(); (OnSetRemind as DelegateCommand).RaiseCanExecuteChanged(); (OnViewAllPlan as DelegateCommand).RaiseCanExecuteChanged(); (OnViewSingleHistory as DelegateCommand).RaiseCanExecuteChanged(); }
void PlanListWindow_Closed(object sender, EventArgs e) { PlanListEditWindow planListWindow = sender as PlanListEditWindow; if (planListWindow.DialogResult == true) { SaveFileDialog sDialog = new SaveFileDialog(); sDialog.Filter = "Excel Files(*.xls)|*.xls"; if (sDialog.ShowDialog() == true) { try { string versionId = "文件编号:"; if (null != planListWindow.planExtraEntity && null != planListWindow.planExtraEntity.FileId) { versionId += planListWindow.planExtraEntity.FileId; } versionId += " 计划版本:"; versionId += SelectProjectEntity.PlanVersionID; string projectNameKey = SelectProjectEntity.ProjectName + " "; string manufactureNumber = "生产令号:" + SelectProjectEntity.ManufactureNumber; Workbook workbook = new Workbook(); ColumnModel columnModel = new ColumnModel(); foreach (PlanListViewModel item in planListWindow.planListViewModelList) { string projectNameName = projectNameKey + item.Title; Worksheet worksheet = new Worksheet(item.Title); Int16 RowCount = 0; worksheet.Cells[RowCount++, 0] = new Cell(versionId); worksheet.Cells[RowCount++, 0] = new Cell(projectNameName); worksheet.Cells[RowCount++, 0] = new Cell(manufactureNumber); int columnCount = 0; foreach (string itemColumn in columnModel.List[item.ColumnModelIndex]) { worksheet.Cells[RowCount, columnCount++] = new Cell(itemColumn); } ++RowCount; foreach (PlanEntity planEntity in item.PlanList) { columnCount = 0; string value = Convert.ToString(planEntity.SequenceId); worksheet.Cells[RowCount, columnCount++] = new Cell(value); worksheet.Cells[RowCount, columnCount++] = new Cell(planEntity.ComponentName); worksheet.Cells[RowCount, columnCount++] = new Cell(planEntity.TaskDescription); value = Convert.ToString(planEntity.Weight); worksheet.Cells[RowCount, columnCount++] = new Cell(value); if (planEntity.Score.HasValue) { value = Convert.ToString(planEntity.Score.Value); worksheet.Cells[RowCount, columnCount] = new Cell(value); } ++columnCount; if (1 == item.ColumnModelIndex) { if (planEntity.OrderDate.HasValue) { value = Convert.ToString(planEntity.OrderDate.Value); worksheet.Cells[RowCount, columnCount] = new Cell(value); } ++columnCount; } value = Convert.ToString(planEntity.TargetDate); worksheet.Cells[RowCount, columnCount++] = new Cell(value); if (planEntity.TargetDateAdjustment1.HasValue) { value = Convert.ToString(planEntity.TargetDateAdjustment1.Value); worksheet.Cells[RowCount, columnCount] = new Cell(value); } ++columnCount; if (planEntity.TargetDateAdjustment2.HasValue) { value = Convert.ToString(planEntity.TargetDateAdjustment2.Value); worksheet.Cells[RowCount, columnCount] = new Cell(value); } ++columnCount; if (planEntity.AccomplishDate.HasValue) { value = Convert.ToString(planEntity.AccomplishDate.Value); worksheet.Cells[RowCount, columnCount] = new Cell(value); } ++columnCount; if (null != planEntity.DepartmentName && string.Empty != planEntity.DepartmentName) { worksheet.Cells[RowCount, columnCount] = new Cell(planEntity.DepartmentName); } ++columnCount; if (null != planEntity.Remark && string.Empty != planEntity.Remark) { worksheet.Cells[RowCount, columnCount] = new Cell(planEntity.Remark); } ++columnCount; ++RowCount; } workbook.Worksheets.Add(worksheet); } Stream sFile = sDialog.OpenFile(); workbook.Save(sFile); Message.InfoMessage("导出成功"); } catch (Exception outputE) { string errorMessage = "导出文件失败:" + outputE.Message; Message.ErrorMessage(errorMessage); } } } (OnExportPlan as DelegateCommand).RaiseCanExecuteChanged(); }
private void OnExportCommand() { SaveFileDialog sDialog = new SaveFileDialog(); sDialog.Filter = "Excel Files(*.xls)|*.xls"; if (sDialog.ShowDialog() == true) { try { Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet("外购部件汇总"); //Title worksheet.Cells[0, 0] = new Cell("生产令号"); worksheet.Cells[0, 1] = new Cell("项目名称"); worksheet.Cells[0, 2] = new Cell("主要部件名称"); worksheet.Cells[0, 3] = new Cell("厂家"); worksheet.Cells[0, 4] = new Cell("到货时间"); Int16 RowCount = 1; foreach (ImportantPartEntity importantPartEntity in ImportantPartEntityList) { int columnCount = 0; if (ProjectNameSearch != null && ProjectNameSearch.Length > 0) { if (!importantPartEntity.ProjectName.Contains(ProjectNameSearch)) { continue; } } worksheet.Cells[RowCount, columnCount++] = new Cell(importantPartEntity.ManufactureNumber); worksheet.Cells[RowCount, columnCount++] = new Cell(importantPartEntity.ProjectName); worksheet.Cells[RowCount, columnCount++] = new Cell(importantPartEntity.ImportantPartName); worksheet.Cells[RowCount, columnCount++] = new Cell(importantPartEntity.ImportantPartManufacturers); worksheet.Cells[RowCount, columnCount++] = new Cell(importantPartEntity.AriveTime.HasValue?importantPartEntity.AriveTime.Value.ToShortDateString():""); RowCount++; } workbook.Worksheets.Add(worksheet); Stream sFile = sDialog.OpenFile(); workbook.Save(sFile); ProductManager.Controls.Message.InfoMessage("导出成功"); } catch (Exception outputE) { string errorMessage = "导出文件失败:" + outputE.Message; ProductManager.Controls.Message.ErrorMessage(errorMessage); } } }
private void OnExportCommand() { SaveFileDialog sDialog = new SaveFileDialog(); sDialog.Filter = "Excel Files(*.xls)|*.xls"; if (sDialog.ShowDialog() == true) { try { Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet("生产令号汇总"); //Title worksheet.Cells[0, 0] = new Cell("生产令号"); worksheet.Cells[0, 1] = new Cell("项目名称"); worksheet.Cells[0, 2] = new Cell("型号"); worksheet.Cells[0, 3] = new Cell("备注"); worksheet.Cells[0, 4] = new Cell("年份"); worksheet.Cells[0, 5] = new Cell("记录时间"); worksheet.Cells[0, 6] = new Cell("删除标记"); worksheet.Cells[0, 7] = new Cell("冻结标记"); worksheet.Cells[0, 8] = new Cell("发运完成时间"); worksheet.Cells[0, 9] = new Cell("项目合同号"); worksheet.Cells[0, 10] = new Cell("开票完成时间"); Int16 RowCount = 1; foreach (ProjectEntity projectEntity in ProjectEntityList) { int columnCount = 0; worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.ManufactureNumber); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.ProjectName); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.ModelNumber); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.Remark); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.YearNumber.ToString()); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.RecordDateString); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.IsDeleteString); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.IsFreezeString); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.DeliveryTime.HasValue? projectEntity.DeliveryTime.Value.ToShortDateString():""); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.ContractNumber); worksheet.Cells[RowCount, columnCount++] = new Cell(projectEntity.InvoiceCompletionTime.HasValue? projectEntity.InvoiceCompletionTime.Value.ToShortDateString():""); RowCount++; } workbook.Worksheets.Add(worksheet); Stream sFile = sDialog.OpenFile(); workbook.Save(sFile); Message.InfoMessage("导出成功"); } catch (Exception outputE) { string errorMessage = "导出文件失败:" + outputE.Message; Message.ErrorMessage(errorMessage); } } }
//==================================================================================================================================== public static void YtgYtdExportToExcel(DataGrid theGrid) { // open file dialog to select an export file. SaveFileDialog sDialog = new SaveFileDialog(); sDialog.Filter = "Excel Files(*.xls)|*.xls"; if (sDialog.ShowDialog() == true) { // create an instance of excel workbook Workbook workbook = new Workbook(); // create a worksheet object Worksheet worksheet = new Worksheet("Data"); Int16 ColumnCount = 0; Int16 RowCount = 0; //Writing Column Names foreach (DataGridColumn dgcol in theGrid.Columns) { string header =""; if (ColumnCount == 0) { Setter setter = (Setter)dgcol.HeaderStyle.Setters[0]; ControlTemplate tem = (ControlTemplate)setter.Value; Grid contentGrid = (Grid)GetTemplateChildByName(theGrid, "ContentGrid"); StackPanel stack = (StackPanel)contentGrid.Children[0]; header = (stack.Children[0] as TextBlock).Text + "\n" + (stack.Children[1] as TextBlock).Text+ "\n" + (stack.Children[2] as TextBlock).Text; } if(dgcol.Header == null) dgcol.Header = header; worksheet.Cells[0, ColumnCount] = new Cell(dgcol.Header.ToString()); ColumnCount++; } //Extracting values from grid and writing to excell sheet // foreach (object data in theGrid.ItemsSource) { ColumnCount = 0; RowCount++; foreach (DataGridColumn col in theGrid.Columns) { string strValue = ""; Binding objBinding = null; if (col is DataGridBoundColumn) objBinding = (col as DataGridBoundColumn).Binding; if (col is DataGridTemplateColumn) { //This is a template column... let us see the underlying dependency object DependencyObject objDO = (col as DataGridTemplateColumn).CellTemplate.LoadContent(); FrameworkElement oFE = (FrameworkElement)objDO; FieldInfo oFI = oFE.GetType().GetField("TextProperty"); if (oFI != null) { if (oFI.GetValue(null) != null) { if (oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)) != null) objBinding = oFE.GetBindingExpression((DependencyProperty)oFI.GetValue(null)).ParentBinding; } } } if (objBinding != null) { if (objBinding.Path.Path != "") { PropertyInfo pi = data.GetType().GetProperty(objBinding.Path.Path); if (pi != null) strValue = Convert.ToString(pi.GetValue(data, null)); } if (objBinding.Converter != null) { if (strValue != "") strValue = objBinding.Converter.Convert(strValue, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString(); //else // strValue = objBinding.Converter.Convert(data, typeof(string), objBinding.ConverterParameter, objBinding.ConverterCulture).ToString(); } } // writing extracted value in excell cell Cell cell = new Cell(strValue); cell.Style = new CellStyle { BackColor = Colors.Orange, RichTextFormat = new Lite.ExcelLibrary.BinaryFileFormat.RichTextFormat(2)}; worksheet.Cells[RowCount, ColumnCount] = cell; ColumnCount++; } } //add worksheet to workbook workbook.Worksheets.Add(worksheet); // get the selected file's stream Stream sFile = sDialog.OpenFile(); workbook.Save(sFile); } }
private void OnExportCommand() { SaveFileDialog sDialog = new SaveFileDialog(); sDialog.Filter = "Excel Files(*.xls)|*.xls"; if (sDialog.ShowDialog() == true) { try { Workbook workbook = new Workbook(); Worksheet worksheet = new Worksheet("负责人汇总"); //Title worksheet.Cells[0, 0] = new Cell("项目负责人"); worksheet.Cells[0, 1] = new Cell("生产令号"); worksheet.Cells[0, 2] = new Cell("项目名称"); worksheet.Cells[0, 3] = new Cell("项目备注"); worksheet.Cells[0, 4] = new Cell("记录时间"); worksheet.Cells[0, 5] = new Cell("发运完成时间"); Int16 RowCount = 1; foreach (ResponsiblePersonEntity responsiblePersonEntity in ResponsiblePersonEntityList) { int columnCount = 0; worksheet.Cells[RowCount, columnCount++] = new Cell(responsiblePersonEntity.ResponsiblePerson); worksheet.Cells[RowCount, columnCount++] = new Cell(responsiblePersonEntity.ManufactureNumber); worksheet.Cells[RowCount, columnCount++] = new Cell(responsiblePersonEntity.ProjectName); worksheet.Cells[RowCount, columnCount++] = new Cell(responsiblePersonEntity.ProjectNote); worksheet.Cells[RowCount, columnCount++] = new Cell(responsiblePersonEntity.RecoderDateTime.HasValue ? responsiblePersonEntity.RecoderDateTime.Value.ToShortDateString() : ""); worksheet.Cells[RowCount, columnCount++] = new Cell(responsiblePersonEntity.OutputDateTime.HasValue ? responsiblePersonEntity.OutputDateTime.Value.ToShortDateString() : ""); RowCount++; } workbook.Worksheets.Add(worksheet); Stream sFile = sDialog.OpenFile(); workbook.Save(sFile); Message.InfoMessage("导出成功"); } catch (Exception outputE) { string errorMessage = "导出文件失败:" + outputE.Message; Message.ErrorMessage(errorMessage); } } }