使用列位置错误将数据从datagridview导出到C#中的Excel [英] Export data from datagridview to Excel in C# with Column position Mistake
本文介绍了使用列位置错误将数据从datagridview导出到C#中的Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好!
当我将数据从datagridview导出到c#中的excel时,我犯了一个错误!
我在datagridview的最后一列中进行了列计算(不在数据库中).我可以在界面中看到它,但是当我使用命令导出数据到excel时,则在第一列进行列计算.您能帮忙在最后一栏进行设置吗?请
这是我使用export到excel的代码.
Hi everyone!
When I export data from datagridview to excel in c# i have a mistake!
The column calculation (it''s not in database) I put at column last in datagridview. I can see it in interface, but when I used command export data to excel then the column calculation at the column frist. Can you help set it at column last? Plese .
This is my code to use export to excel.
static public bool exportDataToExcel(string caption, DataGridView grv)
{
bool result = false;
Excel.ApplicationClass xlApp;
Excel.Worksheet xlSheet;
Excel.Workbook xlBook;
object missValue = System.Reflection.Missing.Value;
xlApp = new Excel.ApplicationClass();
xlBook = xlApp.Workbooks.Add(missValue);
xlSheet = (Excel.Worksheet)xlBook.Worksheets.get_Item(1);
xlApp.Visible = false;
int socot = grv.Columns.GetColumnCount(DataGridViewElementStates.Displayed);
int sohang = grv.Rows.Count;//dt.Rows.Count;//
int i,j;
SaveFileDialog f = new SaveFileDialog();
f.Filter = "Excel file (*.xls)|*.xls";
if (f.ShowDialog() == DialogResult.OK)
{
xlSheet.get_Range("B1", Convert.ToChar(socot + 65) + "1").Merge(false);
Excel.Range caption = xlSheet.get_Range("B2", Convert.ToChar(socot + 65) + "1");
caption.Select();
caption.FormulaR1C1 = caption;
caption.HorizontalAlignment = Excel.Constants.xlCenter;
caption.Font.Bold = true;
caption.VerticalAlignment = Excel.Constants.xlCenter;
caption.Font.Size = 16;
caption.Font.Name = "Arial";
caption.Font.ColorIndex = 3;
caption.Interior.ColorIndex = 6;
caption.RowHeight = 25;
Excel.Range header = xlSheet.get_Range("B2", Convert.ToChar(socot + 65) + "2");
header.Select();
header.HorizontalAlignment = Excel.Constants.xlCenter;
header.Font.Bold = true;
header.Font.Size = 12;
header.Font.ColorIndex=2;
header.Font.Name = "Arial";
header.Interior.ColorIndex = 25;
for (i = 0; i<socot ; i++)
{
xlSheet.Cells[2, i + 2] = grv.Columns[i].HeaderText;
}
for (i = 0; i < sohang; i++)
for (j = 0; j < socot; j++)
{
xlSheet.Cells[i + 3, j + 2] = grv.Rows[i].Cells[j].Value.ToString();//dt.Rows[i][j].ToString();//;
((Excel.Range)xlSheet.Cells[i + 3, j + 2]).Borders.ColorIndex = 25;
((Excel.Range)xlSheet.Cells[i + 3, j+2]).VerticalAlignment = Excel.Constants.xlCenter;
if (j == 0 || j == 1 || j == 7)
((Excel.Range)xlSheet.Cells[i + 3, j + 2]).HorizontalAlignment = Excel.Constants.xlCenter;
if (j == 3)
{
((Excel.Range)xlSheet.Cells[i + 3, j + 2]).HorizontalAlignment = Excel.Constants.xlLeft;
}
((Excel.Range)xlSheet.Cells[i + 3, 7]).WrapText = true;
}
for (i = 0; i < socot; i++)
{
((Excel.Range)xlSheet.Cells[1, i + 1]).EntireColumn.AutoFit();
}
//save file
xlBook.SaveAs(f.FileName, Excel.XlFileFormat.xlWorkbookNormal, missValue, missValue, missValue, missValue, Excel.XlSaveAsAccessMode.xlExclusive, missValue, missValue, missValue, missValue, missValue);
xlBook.Close(true, missValue, missValue);
xlApp.Quit();
releaseObject(xlSheet);
releaseObject(xlBook);
releaseObject(xlApp);
result = true;
}
return result;
}
推荐答案
我已经完成了:D
感谢您的帮助
I have done yet :D
Thanks for helping me
我是ghotgewadi dodamarg的uday satardekar.
请告诉我详细如何在excel中保存网格列
I am uday satardekar from ghotgewadi dodamarg.
please tell me how in details how to save grid columns in excel
这篇关于使用列位置错误将数据从datagridview导出到C#中的Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文