Stringbuilder跳过行 [英] Stringbuilder skips rows
本文介绍了Stringbuilder跳过行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个将gridview中的数据导出到excel电子表格的函数。我的问题是,当我导出大量行时,它会跳过行。这是我的代码,非常感谢任何输入。
Hi,
I have created a function that exports a data from gridview to an excel spreadsheet. My problem is that it skips row when I export large number of rows. Here is my code, any inputs are greatly appreciated.
private void ToExcel()
{
string sep = "";
//grdExcel.DataBind();
System.Text.StringBuilder strbldr = new System.Text.StringBuilder();
for (int i = 0; i < grdExcel.Columns.Count; i++)
{
sep = "\t";
//separting header columns text with comma operator
strbldr.Append(grdExcel.Columns[i].HeaderText + sep);
}
//appending new line for gridview header row
strbldr.Append("\n");
for (int j = 0; j < grdExcel.Rows.Count; j++)
{
sep = "";
for (int k = 0; k < grdExcel.Columns.Count; k++)
{
//separating gridview columns with comma
if (grdExcel.Rows[j].Cells[k].Text == " " || grdExcel.Rows[j].Cells[k].Text == "")
{
//InsertFormula(j, k);//insert formula in excel
strbldr.Append(sep + InsertFormula(j, k));
}
else
{
strbldr.Append(sep + grdExcel.Rows[j].Cells[k].Text);
}
sep = " ";
}
//appending new line for gridview rows
strbldr.Append("\n");
}
Response.Clear();
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=ADS_Detailed_reports_"+System.DateTime.Now.ToString("d-M-yyyy HH:mm:ss").Replace(":", "-")+".xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
Response.Buffer = true;
grdExcel.AllowPaging = false;
Response.Write(strbldr.ToString());
Response.Flush();
Response.End();
谢谢,
Franco
Thanks,
Franco
推荐答案
这对我来说有点愚蠢,但我能够得到一个解决方案当我调试时,我发现一些文本的末尾有一个回车符(\\\\ n),导致行停止。我刚删除它们,现在工作正常。
谢谢,
Hi,
This is kind of stupid of me, but I was able to get a solution. When I was debugging, i found out that some of the text has a carriage return character ("\r\n") on the end which causes the lines to go down. I just removed them and it's working fine now.
Thanks,
try
{
Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);
int i;
int ii;
int j;
for (i = 1; i < dgvAssetResult.Columns.Count + 1; i++)
{
ExcelApp.Cells[1, i] = dgvAssetResult.Columns[i - 1].HeaderText;
}
for (ii = 0; ii < dgvAssetResult.Rows.Count; ii++)
{
for (j = 0; j < dgvAssetResult.Columns.Count; j++)
{
ExcelApp.Cells[ii + 2, j + 1] = dgvAssetResult.Rows[ii].Cells[j].Value.ToString();
}
}
string ls_FileName ="E:\Test.xls";
ExcelApp.ActiveWorkbook.SaveCopyAs(ls_FileName);
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
}
catch
{
}
< br $> b $ b
点击按钮使用此方法。添加适当的参考文献
Using this method on button click. Add the proper references
这篇关于Stringbuilder跳过行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文