首页 新闻 赞助 找找看

求救:如何将 Datagridview中的数据 按显示顺序导出到Excel

0
悬赏园豆:20 [已解决问题] 解决于 2011-11-21 14:11

如上图:datagridview 经过 DisplaIndex 排序后 怎么按显示的顺序导出到Excel

问题补充:

datagridview 有没有类似于 最终视图 的方法呢?

爱睡的猫的主页 爱睡的猫 | 初学一级 | 园豆:187
提问于:2011-11-18 16:44
< >
分享
最佳答案
0

我修改一下上面那位的代码不知道行不行,修改部分:

for (int j = 1; j  < m_DataView.Columns.Count; j++) 

改为:

var columns = dataView.Columns.Cast<DataGridViewColumn>().OrderBy(c => c.DisplayIndex);
foreach (var item in columns) {
//.......
}

就是按DisplayIndex排了一下序.

收获园豆:10
zhangweiwen | 小虾三级 |园豆:904 | 2011-11-18 18:11

你这是asp中的方法?

我在winform中好像不行!

爱睡的猫 | 园豆:187 (初学一级) | 2011-11-18 21:10

@爱睡的猫: 

就是Winform的,那里不行了.

zhangweiwen | 园豆:904 (小虾三级) | 2011-11-19 01:32

@zhangweiwen: 

View Code
 private void DataDiaplayOrderToExcel(DataGridView ordgv)
{
string[] array = new string[ordgv.Columns.Count];

//获取Visble =true 的列
foreach (DataGridViewColumn column in ordgv.Columns)
{
if (column.Visible == true)
{
array[column.DisplayIndex] = column.HeaderText;
}
}
if (array.Length <= 0)
{
MessageBox.Show("NO Data");
return;
}
DataGridView dgv = new DataGridView();
dgv = ordgv;

//创建并打开Excel
Excel.Application myExcel = new Excel.Application();
myExcel.Application.Workbooks.Add(true);
myExcel.Visible = true;

int RowsCount = dgv.Rows.Count;
int ColumnsCount = array.Length;
int mm = 1;
for (int i = 0; i < ColumnsCount; i++)
{
string ColumnName;
try
{
ColumnName = array.GetValue(i).ToString();
}
catch
{
continue;
}
//导出列名
myExcel.Cells[1, mm] = ColumnName;
//导出列内容
for (int m = 0; m < RowsCount; m++)
{
try
{
myExcel.Cells[m + 2, mm] = dgv.Rows[m].Cells[ColumnName].Value.ToString();
}
catch
{ }
}
//执行完一列 mm++
mm++;

}

myExcel.Columns.EntireColumn.AutoFit();


}

上面的代码是我经过测试可以用的,你用的是不是vs2008或2010?我用2005调试你的不成功,不过还是谢谢了!如果你有更好的方法请不吝赐教!

爱睡的猫 | 园豆:187 (初学一级) | 2011-11-21 14:07
其他回答(1)
0
//直接调用就OK
public void DataToExcel(DataGridView m_DataView)
{
SaveFileDialog kk = new SaveFileDialog();
kk.Title = "保存EXECL文件";
kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*";
kk.FilterIndex = 1;
if (kk.ShowDialog() == DialogResult.OK)
{
string FileName = kk.FileName + ".xls";
if (File.Exists(FileName))
File.Delete(FileName);
FileStream objFileStream;
StreamWriter objStreamWriter;
string strLine = "";
objFileStream = new FileStream(FileName, FileMode.OpenOrCreate, FileAccess.Write);
objStreamWriter = new StreamWriter(objFileStream, System.Text.Encoding.Unicode);
for (int i = 0; i < m_DataView.Columns.Count; i++)
{
if (m_DataView.Columns[i].Visible == true)
{
strLine = strLine + m_DataView.Columns[i].HeaderText.ToString() + Convert.ToChar(9);
}
}
objStreamWriter.WriteLine(strLine);
strLine = "";

for (int i = 0; i < m_DataView.Rows.Count; i++)
{
if (m_DataView.Columns[0].Visible == true)
{
if (m_DataView.Rows[i].Cells[0].Value == null)
strLine = strLine + "" + Convert.ToChar(9);
else
strLine = strLine + m_DataView.Rows[i].Cells[0].Value.ToString() + Convert.ToChar(9);
}
for (int j = 1; j < m_DataView.Columns.Count; j++)
{
if (m_DataView.Columns[j].Visible == true)
{
if (m_DataView.Rows[i].Cells[j].Value == null)
strLine = strLine + "" + Convert.ToChar(9);
else
{
string rowstr = "";
rowstr = m_DataView.Rows[i].Cells[j].Value.ToString();
if (rowstr.IndexOf("\r\n") > 0)
rowstr = rowstr.Replace("\r\n", "");
if (rowstr.IndexOf("\t") > 0)
rowstr = rowstr.Replace("\t", "");
strLine = strLine + rowstr + Convert.ToChar(9);
}
}
}
objStreamWriter.WriteLine(strLine);
strLine = "";
}
objStreamWriter.Close();
objFileStream.Close();
MessageBox.Show(this,"保存EXCEL成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
}

试试吧

收获园豆:10
artwl | 园豆:16736 (专家六级) | 2011-11-18 16:51

我测试了,这样是不行的,因为datagridview 的 displayIndex 与它 本身实际的 id 是不一样的,

导出时是按照它的实际id 顺序导出的。

不过也谢谢啦!

继续等待 答案..................

支持(0) 反对(0) 爱睡的猫 | 园豆:187 (初学一级) | 2011-11-18 17:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册