2个回答
展开全部
public void outtoexcel(DataGridView dataGridView1,SaveFileDialog saveFileDialog1,string[] stri)
{
//SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "保存Excel表格";
saveFileDialog1.FileName = "导出数据";
saveFileDialog1.Filter = "Excel表格(*.xls)|*.xls";
saveFileDialog1.RestoreDirectory = true;
//saveFileDialog1.CreatePrompt = true;
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Stream myStream;
myStream = saveFileDialog1.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
string str = "";
try
{
sw.WriteLine("\t\t\t\t"+stri[0]);//入库出库单
for (int k = 1; k < 5; k++)//信息
{
if (k > 1)
str += "\t";
str += stri[k];
}
sw.WriteLine(str);
str = null;
for (int k = 5; k < 13; k++)
{
if (k > 5)
str += "\t";
if (k == 3)
str += "\t\t\t";
str += stri[k];
}
sw.WriteLine(str);
str = null;
//写标题
int i = 0;
foreach (DataGridViewColumn one in dataGridView1.Columns)
{
if (i > 0)
{
str += "\t";
}
str += one.HeaderText;
i++;
}
str += "\t备注";
sw.WriteLine(str);
//写内容
foreach (DataGridViewRow one in dataGridView1.Rows)
{
string tempStr = "";
int j = 0;
foreach (DataGridViewCell one2 in one.Cells)
{
if (j > 0)
{
tempStr += "\t";
}
tempStr += one2.EditedFormattedValue.ToString();
j++;
}
tempStr += "\t"+stri[14];
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
}
{
//SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Title = "保存Excel表格";
saveFileDialog1.FileName = "导出数据";
saveFileDialog1.Filter = "Excel表格(*.xls)|*.xls";
saveFileDialog1.RestoreDirectory = true;
//saveFileDialog1.CreatePrompt = true;
if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
{
Stream myStream;
myStream = saveFileDialog1.OpenFile();
StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312"));
string str = "";
try
{
sw.WriteLine("\t\t\t\t"+stri[0]);//入库出库单
for (int k = 1; k < 5; k++)//信息
{
if (k > 1)
str += "\t";
str += stri[k];
}
sw.WriteLine(str);
str = null;
for (int k = 5; k < 13; k++)
{
if (k > 5)
str += "\t";
if (k == 3)
str += "\t\t\t";
str += stri[k];
}
sw.WriteLine(str);
str = null;
//写标题
int i = 0;
foreach (DataGridViewColumn one in dataGridView1.Columns)
{
if (i > 0)
{
str += "\t";
}
str += one.HeaderText;
i++;
}
str += "\t备注";
sw.WriteLine(str);
//写内容
foreach (DataGridViewRow one in dataGridView1.Rows)
{
string tempStr = "";
int j = 0;
foreach (DataGridViewCell one2 in one.Cells)
{
if (j > 0)
{
tempStr += "\t";
}
tempStr += one2.EditedFormattedValue.ToString();
j++;
}
tempStr += "\t"+stri[14];
sw.WriteLine(tempStr);
}
sw.Close();
myStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
sw.Close();
myStream.Close();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询