excel操作实例源码using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Drawing;using QiHe.Office.CompoundDocumentFormat;namespace QiHe.Office.Excel{ public class Workbook { public List<Record> Records = new List<Record>(); public List<Worksheet> Worksheets = new List<Worksheet>(); List<BOUNDSHEET> BoundSheets = new List<BOUNDSHEET>(); public SST SharedStringTable; public MSODRAWINGGROUP DrawingGroup; public DateTime BaseDate; public ColorPalette ColorPalette = new ColorPalette(); public List<XF> ExtendedFormats = new List<XF>(); /// <summary> /// Open workbook from a file stream. /// </summary> /// <param name="file"></param> public void Open(Stream file) { CompoundDocument doc = CompoundDocument.Read(file); if (doc == null) throw new Exception("Invalid Excel file"); byte[] bookdata = doc.GetStreamData("Workbook"); this.Read(new MemoryStream(bookdata)); } public void Read(Stream stream) { ReadRecords(stream); DecodeRecords(); foreach (BOUNDSHEET boundSheet in BoundSheets) { stream.Position = boundSheet.StreamPosition; Worksheet sheet = Worksheet.Read(stream); sheet.Book = this; sheet.Name = boundSheet.SheetName; sheet.SheetType = (SheetType)boundSheet.SheetType; sheet.PopulateCells(); Worksheets.Add(sheet); } } private void ReadRecords(Stream stream) { Record record = Record.Read(stream); record.Decode(); Record last_record = record; if (record is BOF && ((BOF)record).StreamType == StreamType.WorkbookGlobals) { while (record.Type != RecordType.EOF) { if (record.Type == RecordType.CONTINUE) { last_record.ContinuedRecords.Add(record); } else { switch (record.Type) { case RecordType.MSODRAWINGGROUP: if (DrawingGroup == null) { DrawingGroup = record as MSODRAWINGGROUP; Records.Add(record); } else { DrawingGroup.ContinuedRecords.Add(record); } break; default: Records.Add(record); break; } last_record = record; } record = Record.Read(stream); } } else { throw new Exception("Invalid Workbook."); } } private void DecodeRecords() { foreach (Record record in Records) { record.Decode(); switch (record.Type) { case RecordType.BOUNDSHEET: BoundSheets.Add(record as BOUNDSHEET); break; case RecordType.XF: ExtendedFormats.Add(record as XF); break; case RecordType.SST: SharedStringTable = record as SST; break; case RecordType.DATEMODE: DATEMODE dateMode = record as DATEMODE; switch (dateMode.Mode) { case 0: BaseDate = DateTime.Parse("1899-12-31"); break; case 1: BaseDate = DateTime.Parse("1904-01-01"); break; } break; case RecordType.PALETTE: PALETTE palette = record as PALETTE; int colorIndex = 8; foreach (int color in palette.RGBColours) { ColorPalette[colorIndex] = Color.FromArgb(color); colorIndex ; } break; } } } public List<byte[]> ExtractImages() { List<byte[]> Images = new List<byte[]>(); if (DrawingGroup != null) { MsofbtDggContainer dggContainer = DrawingGroup.EscherRecords[0] as MsofbtDggContainer; foreach (MsofbtBSE blipStoreEntry in dggContainer.BstoreContainer.EscherRecords) { if (blipStoreEntry.BlipRecord == null) continue; Images.Add(blipStoreEntry.ImageData); } } return Images; } public byte[] ExtractImage(int index, out ushort type) { if (DrawingGroup != null) { MsofbtDggContainer dggContainer = DrawingGroup.EscherRecords[0] as MsofbtDggContainer; MsofbtBSE blipStoreEntry = dggContainer.BstoreContainer.EscherRecords[index] as MsofbtBSE; if (blipStoreEntry.BlipRecord != null) { type = blipStoreEntry.BlipRecord.Type; return blipStoreEntry.ImageData; } } type = 0; return null; } }}
下载C# 读取操作excel文件用户还喜欢
- 18480 文章数
- 500万+ 热度
作者专栏
编辑推荐
- 淡抹u2引擎,修复内容较多,物有所值
- 界域传说·经典巨作=传世单机(一键安装)
- 丸子版本(175个传世版本大集合)
- GS版本:神话公益服务端+客户端
- 图片放大工具(放大图片不模糊)
- 剪映无限制VIP版
- 传奇世界客户端下载器,史上最全传世客户端
- 传世GS20220920商业引擎注册+登录配置器 解压密码是1
- U2官方排行榜游戏网关 支持元神,支持传家宝
- GS开战传世客户端+服务端
- (淡漠夕阳)u2引擎合区工具
- 传世GS引擎消除“你的游戏客户端版本号过旧,请及时更新”提示
- 传世一机多区双线路配置器--免密码版本
- 传世凤凰登陆器劫持修复软件
- SQLite3 for Navicat
- 传奇世界npc对话框编辑工具
- 传世GS落霞铭文服务器端
- gs_20210409引擎包+注册机(无限制)
- 传奇世界NPC对话封包查看器[支持时长版和极速版]
- 彩虹引擎传世脚本编辑工具1.7版来了,支持函数脚本翻译
评论