【发布时间】:2013-03-20 07:22:05
【问题描述】:
我正在尝试将数据表导出到我的应用程序中的 excel 中。但是我的数据表包含超过 220 万条记录,我只能将 65536 条记录写入 excel。
google了很多,才知道2007年以前的版本只能写65536条记录。
如果我们可以将超过 65536 条记录写入 excel,请告诉我
我没有在我的应用程序中使用 microsoft office lib 我正在使用我自己的一个类,它将一个表格写入 excel
这是我的excel课
公共类 ExcelWrite { 私有流流; 私人 BinaryWriter 作家;
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]);
}
/// <summary>
/// Initializes a new instance of the <see cref="ExcelWriter"/> class.
/// </summary>
/// <param name="stream">The stream.</param>
public ExcelWrite(Stream stream)
{
this.stream = stream;
writer = new BinaryWriter(stream);
}
public ExcelWrite()
{
}
/// <summary>
/// Writes the text cell value.
/// </summary>
/// <param name="row">The row.</param>
/// <param name="col">The col.</param>
/// <param name="value">The string value.</param>
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);
}
/// <summary>
/// Writes the integer cell value.
/// </summary>
/// <param name="row">The row number.</param>
/// <param name="col">The column number.</param>
/// <param name="value">The value.</param>
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);
}
/// <summary>
/// Writes the double cell value.
/// </summary>
/// <param name="row">The row number.</param>
/// <param name="col">The column number.</param>
/// <param name="value">The value.</param>
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);
}
/// <summary>
/// Writes the empty cell.
/// </summary>
/// <param name="row">The row number.</param>
/// <param name="col">The column number.</param>
public void WriteCell(int row, int col)
{
ushort[] clData = { 0x0201, 6, 0, 0, 0x17 };
clData[2] = (ushort)row;
clData[3] = (ushort)col;
WriteUshortArray(clData);
}
/// <summary>
/// Must be called once for creating XLS file header
/// </summary>
public void BeginWrite()
{
WriteUshortArray(clBegin);
}
/// <summary>
/// Ends the writing operation, but do not close the stream
/// </summary>
public void EndWrite()
{
WriteUshortArray(clEnd);
writer.Flush();
}
public void exporttoExcel(DataTable table,string filename)
{
DataTable dt = new DataTable();
try
{
StringBuilder SB = new StringBuilder();
dt = table;
if (dt.Rows.Count > 0)
{
FileStream stream = File.Open("C:/Application/" + filename + ".xls", FileMode.Create);
ExcelWrite excelWriter = new ExcelWrite(stream);
//AB_MISreports.Util.ExcelWrite excelWriter = new AB_MISreports.Util.ExcelWrite(stream);
excelWriter.BeginWrite();
for (int i = 0; i < dt.Columns.Count; i++)
{
excelWriter.WriteCell(0, i, dt.Columns[i].ColumnName.ToString());
for (int j = 0; j < dt.Rows.Count; j++)
{
excelWriter.WriteCell(j + 1, i, dt.Rows[j][i].ToString());
}
}
excelWriter.EndWrite();
stream.Close();
HttpContext.Current.Response.ContentType = "application/vnd.xls";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename + ".xls");
HttpContext.Current.Response.TransmitFile("C:/Application/" + filename + ".xls");
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
dt.Dispose();
}
}
}
【问题讨论】:
-
如果您的行数很高,请考虑使用数据库。 Excel可以连接sql server作为数据源
-
能否请您发布您正在使用的代码?
-
该限制适用于旧版本的 excel(2003 及更早版本)。考虑写入多张纸或使用更新的版本。
-
@nunespascal 我只从数据库中获取数据表。实际上,我的要求是从 UI 为我的 sql server 中的数据生成报告。
-
@Cherry 200000 条记录对于用户来说在报告中浏览会很乏味。如果要将其导入另一个数据库,则 csv 之类的格式会更简单。