将数据从DataGridView导入到Execel中抛出奇怪异常!! 苦苦思索,毫无进展,希望高手指点
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CangKuGuanli.BLL
{
public class ExportXLS
{
private string tempStr;
private string str;
public void ExportDataGridViewToExcel(DataGridView dataGridview1)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Execl files (*.xls)|*.xls ";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.CreatePrompt = false;
saveFileDialog.Title = "导出Excel文件到 ";
if (saveFileDialog.ShowDialog() == DialogResult.Cancel)
return;
Stream myStream = saveFileDialog.OpenFile();
StreamWriter sw = new StreamWriter(myStream,System.Text.Encoding.GetEncoding( "gb2312 ")); //gb2312
try
{
//写标题
for (int i = 0; i < dataGridview1.ColumnCount; i++)
{
if (i == 0) dataGridview1.Columns[i].HeaderText = "序号 ";
if (i > 0)
{
str += "\t ";