C#生成Excel慢~~~~~啊 小弟做了一个生成报告的程序,要求读取Form上的Textbox,DataGridView等控件的内容,并自动生成报告但是生成报告很慢啊,一个10页的报告要生成1分10秒啊,大侠有碰到过这种情况的吗?有没有什么好的解决办法 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以尝试用Range的Value2属性来实现快速导入 用二维数组,比较快给你发个链接吧。http://blog.csdn.net/xingjunli/archive/2009/07/13/4345069.aspx 用二维数组,比较快 给你发个链接吧。 http://blog.csdn.net/xingjunli/archive/2009/07/13/4345069.aspx vbscript生成大概需要4秒左右 一次性插入效率高,另外,Excel本来就慢一些。 private void Write() { if (dt.Rows[0][0].ToString() == "") { MessageBox.Show("dt is NULL"); return; } Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.Interop.Excel.Application(); Workbooks workBooks = xlsApp.Workbooks; Workbook workBook = workBooks.Add(XlWBATemplate.xlWBATWorksheet); Worksheet workSheet = (Worksheet)workBook.Worksheets[1]; Range range = null; long tbRoC = dt.Rows.Count; int tbCoC = dt.Columns.Count; long rowRead = 0; float percent = 0f; for (int i = 0; i < tbCoC; i++) { workSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName; range = (Range)workSheet.Cells[1, i + 1]; } for (int r = 0; r < tbRoC; r++) { for (int i = 0; i < tbCoC; i++) { workSheet.Cells[r + 2, i + 1] = dt.Rows[r][i]; range = (Range)workSheet.Cells[r + 2, i + 1]; range.EntireColumn.AutoFit(); } percent = ((float)((++rowRead) * 100)) / tbRoC; System.Windows.Forms.Application.DoEvents(); } try { workBook.Saved = true; workBook.SaveCopyAs(txbLJ.Text.Trim()); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error for Save, maybe, this xls is opend"); } finally { workBooks.Close(); if (xlsApp != null) { xlsApp.Workbooks.Close(); xlsApp.Quit(); int gc = GC.GetGeneration(xlsApp); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp); xlsApp = null; GC.Collect(gc); } GC.Collect(); } } 先谢过楼上大侠的代码我的程序就是写DatagridView的时候特别的慢,感觉像人工写入~~~~真没有什么更好的方法吗? 读取到DataGridView中显示,可以用DataSet,把Excel当OLEDB数据库,很快。 我现在就一直用着:FarPoint的FpSpread控件。基本Excel电子表格的功能都有。 不好意思,我表述有误,是从DataGridView写到Excel读取到DataGridView中显示,可以用DataSet,把Excel当OLEDB数据库,很快。这个我体会到了,哈哈,就是这两者的对比让我很不爽啊,一个导入一个导出,一个像死机,一个像发飙加点分,拉升人气~~ 用Ado.net去写,很快,MSDN上面就有4,5种Excel的读写方法Ado.net写快,但是不能自己控制格式office互操作可以直接控制格式,但是很慢第三方组件,要付费,比office互操作要快,但比ado.net要慢,我推荐一个componentOne的Excel组件 告诉你一个方法,一般人不告诉,你先打开Excel,然后把你要的格式做好,如标题,一行数据示范然后别存为XML表格,然后用编辑器打开XML,这时我想你应该知道怎么做了吧,按格式生成文件,然后保存为.xls文件就OK了 using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Windows.Forms;namespace Online_VA{ static class Export2File { public static bool Export2Excel(DataGridView datagridview) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Execl files (*.xls)|*.xls"; saveFileDialog.FilterIndex = 0; saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == DialogResult.OK) { return Export2Excel(saveFileDialog.FileName, datagridview); } else { return false; } } public static bool Export2Excel(string filename, System.Windows.Forms.DataGridView datagridview) { FileStream fs = null; StreamWriter sw = null; try { fs = File.Open(filename, FileMode.Create); sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312")); for (int i = 0; i < datagridview.ColumnCount; i++) { if (i > 0) { sw.Write("\t"); } sw.Write(datagridview.Columns[i].HeaderText); } sw.WriteLine(); foreach (DataGridViewRow row in datagridview.Rows) { for (int i = 0; i < datagridview.ColumnCount; i++) { if (i > 0) { sw.Write("\t"); } sw.Write(row.Cells[i].Value == null ? "" : row.Cells[i].Value.ToString()); } sw.WriteLine(); System.Windows.Forms.Application.DoEvents(); } return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } finally { if (sw != null) { sw.Close(); } if (fs != null) { fs.Close(); } } } }} workBooks.Close(); if (xlsApp != null) { xlsApp.Workbooks.Close(); xlsApp.Quit(); int gc = GC.GetGeneration(xlsApp); System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp); xlsApp = null; GC.Collect(gc); } GC.Collect();顺便问一下啊,这个关闭Excel好用吗? 告诉你一个方法,一般人不告诉,你先打开Excel,然后把你要的格式做好,如标题,一行数据示范 然后别存为XML表格,然后用编辑器打开XML,这时我想你应该知道怎么做了吧,按格式生成文件,然后保存为.xls文件就OK了 我来了,哈哈你先把Excel另存为 xml表格然后把生成的XML 复杂到项目里去用visual打开xml文件,找到节点<Table>将Table的 ss:ExpandedRowCount 属性删掉然后往下看有Row节点,将保存数据的Row,剪切掉,然后在剪切掉的位置输入{0}好了,现在回到导出Excel的位置,定义一个string Row = "刚刚剪切掉的Row";string sRow = @" <Row ss:AutoFitHeight=""0""> <Cell ss:StyleID=""s31""><Data ss:Type=""String"">{0}</Data></Cell> <Cell ss:StyleID=""s31""><Data ss:Type=""String"">{1}</Data></Cell> <Cell ss:StyleID=""s31""><Data ss:Type=""String"">{2}</Data></Cell> <Cell ss:StyleID=""s31""><Data ss:Type=""String"">{3}</Data></Cell> <Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{4}</Data></Cell> <Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{5}</Data></Cell> <Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{6}</Data></Cell> <Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{7}</Data></Cell> <Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{8}</Data></Cell></Row>";循环将Row里的Cell 替换成自己想想的数据并拼接起来用IO读取这个XML,并format {0}参如:StreamWriter writer = null; StreamReader reader = null; try { string fileName = Guid.NewGuid().ToString() + ".xls"; reader = new StreamReader(Server.MapPath("~/section/cantonfair/各展区情况表.xml")); writer = new StreamWriter(Server.MapPath(path + fileName), false, Encoding.UTF8); writer.Write(reader.ReadToEnd(), sheetArray.ToString()); } catch (Exception ex) { MessageBox.Show("导出失败" + ex.Message); return; } finally { if (reader != null) reader.Close(); if (writer != null) writer.Close(); } http://www.cnblogs.com/wuxing/archive/2009/08/28/1555857.html为了你 我写了篇BLOG 没有必要,如果你用JQuery取完数据,然后用Aspose.Cell处理,就完成了可以参考FortuneBase中Aspose.Cell的代码,采用域方式处理参考地址www.cnblogs.com/mail-ricklee 直接写<table><tr><td>像写网页的html一样,要什么格式写什么格式,然后保存为.xls格式就OK用StringBuilder 的 Append 速度快到你不想要 求高手指点!关于获取网页内容的编程 如何获得窗体句柄? datagridview中怎样判断某列已经排序 关于文本中取符合条件的中间一段内容! 点击listview的项得到项的内容 在gridview中如何实现自定义分页? 求C#图像旋转算法 错误 5 找不到类型或命名空间名称“CHCNetSDK”(是否缺少 using 指令或程序集引用?) E:\NT\test\WindowsFormsApplic 怎样保存用户浏览过的文件地址? .net编写的程序为何这么慢? .net 生成excel 时报如下错误。麻烦大家看看 打印问题(急再线等)
给你发个链接吧。
http://blog.csdn.net/xingjunli/archive/2009/07/13/4345069.aspx
给你发个链接吧。
http://blog.csdn.net/xingjunli/archive/2009/07/13/4345069.aspx
大概需要4秒左右
private void Write()
{
if (dt.Rows[0][0].ToString() == "") { MessageBox.Show("dt is NULL"); return; } Microsoft.Office.Interop.Excel.Application xlsApp = new Microsoft.Office.Interop.Excel.Application();
Workbooks workBooks = xlsApp.Workbooks;
Workbook workBook = workBooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet workSheet = (Worksheet)workBook.Worksheets[1];
Range range = null; long tbRoC = dt.Rows.Count;
int tbCoC = dt.Columns.Count; long rowRead = 0;
float percent = 0f; for (int i = 0; i < tbCoC; i++)
{
workSheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
range = (Range)workSheet.Cells[1, i + 1];
} for (int r = 0; r < tbRoC; r++)
{
for (int i = 0; i < tbCoC; i++)
{
workSheet.Cells[r + 2, i + 1] = dt.Rows[r][i];
range = (Range)workSheet.Cells[r + 2, i + 1];
range.EntireColumn.AutoFit();
} percent = ((float)((++rowRead) * 100)) / tbRoC; System.Windows.Forms.Application.DoEvents();
} try
{
workBook.Saved = true;
workBook.SaveCopyAs(txbLJ.Text.Trim());
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Error for Save, maybe, this xls is opend");
}
finally
{
workBooks.Close();
if (xlsApp != null)
{
xlsApp.Workbooks.Close();
xlsApp.Quit(); int gc = GC.GetGeneration(xlsApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
xlsApp = null; GC.Collect(gc);
}
GC.Collect();
}
}
真没有什么更好的方法吗?
读取到DataGridView中显示,可以用DataSet,把Excel当OLEDB数据库,很快。
FarPoint的FpSpread控件。
基本Excel电子表格的功能都有。
读取到DataGridView中显示,可以用DataSet,把Excel当OLEDB数据库,很快。
这个我体会到了,哈哈,就是这两者的对比让我很不爽啊,一个导入一个导出,一个像死机,一个像发飙加点分,拉升人气~~
office互操作可以直接控制格式,但是很慢第三方组件,要付费,比office互操作要快,但比ado.net要慢,我推荐一个componentOne的Excel组件
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;namespace Online_VA
{
static class Export2File
{
public static bool Export2Excel(DataGridView datagridview)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true; if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
return Export2Excel(saveFileDialog.FileName, datagridview);
}
else
{
return false;
}
} public static bool Export2Excel(string filename, System.Windows.Forms.DataGridView datagridview)
{
FileStream fs = null;
StreamWriter sw = null;
try
{
fs = File.Open(filename, FileMode.Create);
sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312")); for (int i = 0; i < datagridview.ColumnCount; i++)
{
if (i > 0)
{
sw.Write("\t");
} sw.Write(datagridview.Columns[i].HeaderText);
}
sw.WriteLine(); foreach (DataGridViewRow row in datagridview.Rows)
{
for (int i = 0; i < datagridview.ColumnCount; i++)
{
if (i > 0)
{
sw.Write("\t");
} sw.Write(row.Cells[i].Value == null ? "" : row.Cells[i].Value.ToString()); } sw.WriteLine(); System.Windows.Forms.Application.DoEvents(); } return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
finally
{
if (sw != null)
{
sw.Close();
}
if (fs != null)
{
fs.Close();
}
}
}
}
}
if (xlsApp != null)
{
xlsApp.Workbooks.Close();
xlsApp.Quit(); int gc = GC.GetGeneration(xlsApp);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlsApp);
xlsApp = null; GC.Collect(gc);
}
GC.Collect();
顺便问一下啊,这个关闭Excel好用吗?
将Table的 ss:ExpandedRowCount 属性删掉然后往下看有Row节点,将保存数据的Row,剪切掉,然后在剪切掉的位置输入{0}好了,现在回到导出Excel的位置,定义一个string Row = "刚刚剪切掉的Row";
string sRow = @" <Row ss:AutoFitHeight=""0"">
<Cell ss:StyleID=""s31""><Data ss:Type=""String"">{0}</Data></Cell>
<Cell ss:StyleID=""s31""><Data ss:Type=""String"">{1}</Data></Cell>
<Cell ss:StyleID=""s31""><Data ss:Type=""String"">{2}</Data></Cell>
<Cell ss:StyleID=""s31""><Data ss:Type=""String"">{3}</Data></Cell>
<Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{4}</Data></Cell>
<Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{5}</Data></Cell>
<Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{6}</Data></Cell>
<Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{7}</Data></Cell>
<Cell ss:StyleID=""s32""><Data ss:Type=""Number"">{8}</Data></Cell>
</Row>";循环将Row里的Cell 替换成自己想想的数据并拼接起来用IO读取这个XML,并format {0}参
如:StreamWriter writer = null;
StreamReader reader = null; try
{
string fileName = Guid.NewGuid().ToString() + ".xls";
reader = new StreamReader(Server.MapPath("~/section/cantonfair/各展区情况表.xml"));
writer = new StreamWriter(Server.MapPath(path + fileName), false, Encoding.UTF8);
writer.Write(reader.ReadToEnd(), sheetArray.ToString());
}
catch (Exception ex)
{
MessageBox.Show("导出失败" + ex.Message);
return;
}
finally
{
if (reader != null)
reader.Close();
if (writer != null)
writer.Close();
}
为了你 我写了篇BLOG
www.cnblogs.com/mail-ricklee
直接写<table><tr><td>像写网页的html一样,要什么格式写什么格式,然后保存为.xls格式就OK
用StringBuilder 的 Append 速度快到你不想要