C#实现excel导出(2)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
}
}
}
//str += dataGridView1.Columns[i].HeaderText;
}
//sw.WriteLine(str);
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = true;
}
else
{
sw.Write("\t" + dataGridView1.Columns[i].HeaderText + ",");
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("GB2312"));
string str = "";
DateTime start = DateTime.Now;
try
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl文件(*.xls)|*.xls";
//tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
}
}
}
else
{
sw.Write("\t" +dataGridView1.Rows[j].Cells[k].Value.ToString());
saveFileDialog.Title = "数据视图导出EXCEL文件";
if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
return;
}
else
{
sw.Write("\t" + dataGridView1.Rows[j].Cells[k].Value.ToString());
{
MessageBox.Show("没有数据不能导出报表");return;
}
else
{
#region
sw.Write("\n" + string.Empty);
//tempStr += string.Empty;
}
if (i == 0)
{
sw.Write(dataGridView1.Columns[i].HeaderText+",");
使用数据流进行导出,速度很快;(注:该方法主要用于txt导出,用于Excel导出时,打开Excel文件会提示文件格式不正确;)
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sender"></param>
Stream myStream;
myStream = saveFileDialog.OpenFile();
//myStream = File.Open(@"e:\wwweeerrr.xls", FileMode.Create);
//{
// tempStr += "\t";
//}
if (k == 0)
try
{
//写标题
for (int i = 0; i < dataGridView1.ColumnCount; i++)
/// <param name="e"></param>
private void btnExportRecords_Click(object sender, EventArgs e)
{
if (this.lvZVeh.Items.Count == 0)
//写内容
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
string tempStr = "";
}
//sw.WriteLine(tempStr);
}
{
if (dataGridView1.Rows[j].Cells[k].Value == null)
{
{
sw.Write("\t" + string.Empty );
//tempStr += string.Empty;
{
//if (i > 0)
//{
// str += "\
if (dataGridView1.Rows[j].Cells[k].Value == null)
{
sw.Write("\n" + string.Empty );
//tempStr += string.Empty;
else
{
sw.Write("\n" + dataGridView1.Rows[j].Cells[k].Value.ToString());
//tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString();
}
}
else if (k == 1)
{
if (dataGridView1.Rows[j].Cells[k].Value == null)
for (int k = 0; k < dataGridView1.Columns.Count; k++)
{
//if (k > 0)
相关文档
最新文档