c#不安装 office 导出excel

两种方式,第一种方式写 xml 比较简单dataset.WriteXml(strXMLFileName, System.Data.XmlWriteMode.WriteSchema);

 

第二种方式摘自国外开源网站 首先得了解excel文件的文件流格式,直接贴代码 public class ExcelWriter { private Stream stream; private BinaryWriter writer; private ushort[] clBegin = { 0x0809, 8, 0, 0x10, 0, 0 }; private ushort[] clEnd = { 0x0A, 00 }; private void WriteUshortArray(ushort[] value) { for (int i = 0; i < value.Length; i++) writer.Write(value[i]); } ///

/// Initializes a new instance of the class. /// /// The stream. public ExcelWriter(Stream stream) { this.stream = stream; writer = new BinaryWriter(stream); } /// /// Writes the text cell value. /// /// The row. /// The col. /// The string value. public void WriteCell(int row, int col, string value) { ushort[] clData = { 0x0204, 0, 0, 0, 0, 0 }; int iLen = value.Length; byte[] plainText = Encoding.ASCII.GetBytes(value); clData[1] = (ushort)(8 + iLen); clData[2] = (ushort)row; clData[3] = (ushort)col; clData[5] = (ushort)iLen; WriteUshortArray(clData); writer.Write(plainText); } /// /// Writes the integer cell value. /// /// The row number. /// The column number. /// The value. public void WriteCell(int row, int col, int value) { ushort[] clData = { 0x027E, 10, 0, 0, 0 }; clData[2] = (ushort)row; clData[3] = (ushort)col; WriteUshortArray(clData); int iValue = (value << 2) | 2; writer.Write(iValue); } /// /// Writes the double cell value. /// /// The row number. /// The column number. /// The value. public void WriteCell(int row, int col, double value) { ushort[] clData = { 0x0203, 14, 0, 0, 0 }; clData[2] = (ushort)row; clData[3] = (ushort)col; WriteUshortArray(clData); writer.Write(value); } /// /// Writes the empty cell. /// /// The row number. /// The column number. public void WriteCell(int row, int col) { ushort[] clData = { 0x0201, 6, 0, 0, 0x17 }; clData[2] = (ushort)row; clData[3] = (ushort)col; WriteUshortArray(clData); } /// /// Must be called once for creating XLS file header /// public void BeginWrite() { WriteUshortArray(clBegin); } /// /// Ends the writing operation, but do not close the stream /// public void EndWrite() { WriteUshortArray(clEnd); writer.Flush(); } }

 

由上面可以知道读取excel同理,去掉开头和结尾

你可能感兴趣的:(office,c#,stream,excel,integer,string)