I have a GridView data with an additional calculated column. I'd like to export GridView to Excel. It returns empty values for the calculated column only. Any help will be appreciated! Thanks in advance.
Here is the code I used to export to Excel.
protected void ExportToExcel(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
Workbook wb = Excel.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)Excel.ActiveSheet;
ws.Cells.Font.Size = 10;
Excel.ScreenUpdating = false;
GridView3.AllowPaging = false;
GridView3.DataBind();
for (int i = 1; i < GridView3.Columns.Count + 1; i++)
{
ws.Cells[1, i] = GridView3.Columns[i - 1].HeaderText;
ws.Cells[1, i].Characters.Font.Bold = true;
}
for (int i = 0; i < GridView3.Rows.Count; i++)
{
for (int j = 0; j < GridView3.Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = HttpUtility.HtmlDecode(GridView3.Rows[i].Cells[j].Text.ToString());
}
}
GridView3.AllowPaging = true;
GridView3.DataBind();
ws.EnableAutoFilter = true;
ws.Range["A1", "A1"].AutoFilter(1, Type.Missing);
ws.Columns.AutoFit();
Excel.ScreenUpdating = true;
Excel.Visible = true;
}
Related
in this part im exporting a DataGridView data and pasting it in a new excel file, my problem is that i wanna export three more DataGridViews to the same excel file and sheet, any suggestion?
private void exportclipboard()
{
dataGridView1.SelectAll();
dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
dataGridView1.MultiSelect = true;
dataGridView1.SelectAll();
DataObject dataobj = dataGridView2.GetClipboardContent();
if (dataobj != null)
Clipboard.SetDataObject(dataobj);
}
private void btn_guardar_Click(object sender, EventArgs e)
{
int i = 0;
if(i == 0)
{
exportclipboard();
Excel.Application excel;
Excel.Workbook workbook;
Excel.Worksheet worksheet;
object missvalue = System.Reflection.Missing.Value;
excel = new Excel.Application();
excel.Visible = true;
workbook = excel.Workbooks.Add(missvalue);
worksheet = (Excel.Worksheet)workbook.Worksheets.get_Item(1); //number of sheet
Excel.Range CR = (Excel.Range)worksheet.Cells[1, 1]; //This is the position where the data will be pasted
CR.Select();
worksheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
i++;
}
}
I found a way to export four DataGridViews to excel with a saveDialog implement, but the loops are slow. Any improvement is welcome.
private void ExportarExcel()
{
// Creating a Excel object.
Excel._Application excel = new Excel.Application();
Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
Excel._Worksheet worksheet = null;
try
{
worksheet = workbook.ActiveSheet;
worksheet.Name = "ExportedFromDatGrid";
int cellRowIndex = 1;
int cellColumnIndex = 1;
//Loop through each row and read value from each column.
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
// Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
if (cellRowIndex == 1)
{
worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Columns[j].HeaderText;
}
else
{
worksheet.Cells[cellRowIndex, cellColumnIndex] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
cellColumnIndex++;
}
cellColumnIndex = 1;
cellRowIndex++;
}
//DATAGRIDVIEW2
int cellRowIndex1 = 1;
int cellColumnIndex1 = 3;
//Loop through each row and read value from each column.
for (int i = 0; i < dataGridView2.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView2.Columns.Count; j++)
{
// Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
if (cellRowIndex1 == 1)
{
worksheet.Cells[cellRowIndex1, cellColumnIndex1] = dataGridView2.Columns[j].HeaderText;
}
else
{
worksheet.Cells[cellRowIndex1, cellColumnIndex1] = dataGridView2.Rows[i].Cells[j].Value.ToString();
}
cellColumnIndex1++;
}
cellColumnIndex1 = 3;
cellRowIndex1++;
}
//DATAGRIDVIEW3
int cellRowIndex2 = 1;
int cellColumnIndex2 = 5;
//Loop through each row and read value from each column.
for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView3.Columns.Count; j++)
{
// Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
if (cellRowIndex2 == 1)
{
worksheet.Cells[cellRowIndex2, cellColumnIndex2] = dataGridView3.Columns[j].HeaderText;
}
else
{
worksheet.Cells[cellRowIndex2, cellColumnIndex2] = dataGridView3.Rows[i].Cells[j].Value.ToString();
}
cellColumnIndex2++;
}
cellColumnIndex2 = 5;
cellRowIndex2++;
}
//DATAGRIDVIEW4
int cellRowIndex3 = 1;
int cellColumnIndex3 = 7;
//Loop through each row and read value from each column.
for (int i = 0; i < dataGridView4.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView4.Columns.Count; j++)
{
// Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
if (cellRowIndex3 == 1)
{
worksheet.Cells[cellRowIndex3, cellColumnIndex3] = dataGridView4.Columns[j].HeaderText;
}
else
{
worksheet.Cells[cellRowIndex3, cellColumnIndex3] = dataGridView4.Rows[i].Cells[j].Value.ToString();
}
cellColumnIndex3++;
}
cellColumnIndex3 = 7;
cellRowIndex3++;
}
//Getting the location and file name of the excel to save from user.
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*";
saveDialog.FilterIndex = 2;
if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveDialog.FileName);
MessageBox.Show("Export Successful");
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
excel.Quit();
workbook = null;
excel = null;
}
}
here is my code. I want to export Column6 and it rows to an Excel sheet. How can i do it?
Excel.Application excel = new Excel.Application();
excel.Visible = true;
object Missing = Type.Missing;
Workbook workbook = excel.Workbooks.Add(Missing);
Worksheet worksheet = (Worksheet)workbook.Sheets[1];
for (int i = 5; i < dataGridView6.Columns.Count; i++)
{
worksheet.Cells[1, 1] = dataGridView6.Columns[i].HeaderText;
}
for (int i = 0; i < dataGridView6.Rows.Count - 1; i++)
{
for (int j = 5; j < dataGridView6.Columns.Count - 1; j++)
{
worksheet.Cells[1, 1] = dataGridView6.Rows[i].Cells[j].Value.ToString();
}
}
}
I'm exporting a datagridview to Excel using a button's click event. The data is exporting but the last row of the table is not appearing. I've played with my code but I'm unable to figure out the problem. If someone would help me I would be grateful.
private void button34_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application Excel = new
Microsoft.Office.Interop.Excel.Application();
Workbook wb = Excel.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)Excel.ActiveSheet;
Excel.Visible = true;
ws.Cells[1, 1] = "VehiclePlateNumber";
ws.Cells[1, 2] = "VehicleDescription";
ws.Cells[1, 3] = "DeviceCode";
ws.Cells[1, 4] = "DriverName";
ws.Cells[1, 5] = "DriverKeyTag";
for (int j = 2; j <= datagridview9.Rows.Count; j++)
{
for (int i = 1; i <= 5; i++)
{
ws.Cells[j, i] = datagridview9.Rows[j - 2].Cells[i - 1].Value;
}
}
}
Please change it to following statement and check once
for (int j = 2; j <= datagridview9.Rows.Count+1; j++)
I'm begining in WPF and i'm working with datagrid
I have multi datagrids. If i click a button ("Export" button) first time, it will create a new excel window and export data to first sheet. Then, i change to another datagrid and click a button ("Export" button) second time. so, it will create a new sheet in excel window which created before. Can you help me change my code ?
Thank you very much!!
public void Export(DataTable dt, string sheetName, string title)
{
Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks oBooks;
Microsoft.Office.Interop.Excel.Sheets oSheets;
Microsoft.Office.Interop.Excel.Workbook oBook;
Microsoft.Office.Interop.Excel.Worksheet oSheet;
Excel.Range _range = null;
oExcel.DisplayAlerts = false;
oExcel.Application.SheetsInNewWorkbook = 1;
oBooks = oExcel.Workbooks;
oBook = (Microsoft.Office.Interop.Excel.Workbook)(oExcel.Workbooks.Add(Type.Missing));
oSheets = oBook.Worksheets;
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oSheets.get_Item(1);
oSheet.Name = sheetName;
Microsoft.Office.Interop.Excel.Range head = oSheet.get_Range("A1", "C1");
head.MergeCells = true;
head.Value2 = title;
head.Font.Bold = true;
head.Font.Name = "Tahoma";
head.Font.Size = "18";
head.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
List<object> objHeaders = new List<object>();
for (int n = 0; n <= dt.Rows.Count; n++)
{
objHeaders.Add(dt.Columns[n].ColumnName);
}
var headerToAdd = objHeaders.ToArray();
_range = oSheet.get_Range("A3", Type.Missing);
_range = _range.get_Resize(dt.Rows.Count, dt.Columns.Count);
_range.ColumnWidth = 30;
_range.set_Value(Type.Missing, headerToAdd);
Excel.Range rowHead = oSheet.get_Range("A3", "C"+dt.Columns.Count);
rowHead.Font.Bold = true;
rowHead.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;
rowHead.Interior.ColorIndex = 15;
rowHead.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
int row = dt.Rows.Count;
int col = dt.Columns.Count;
object[,] arr = new object[row, col];
for (int r = 0; r < dt.Rows.Count; r++)
{
DataRow dr = dt.Rows[r];
for (int c = 0; c < dt.Columns.Count; c++)
{
arr[r, c] = dr[c];
}
}
int rowStart = 4;
int columnStart = 1;
int rowEnd = rowStart + dt.Rows.Count - 1;
int columnEnd = dt.Columns.Count;
Microsoft.Office.Interop.Excel.Range c1 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[rowStart, columnStart];
Microsoft.Office.Interop.Excel.Range c2 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[rowEnd, columnEnd];
Microsoft.Office.Interop.Excel.Range range = oSheet.get_Range(c1, c2);
range.Value2 = arr;
range.Borders.LineStyle = Microsoft.Office.Interop.Excel.Constants.xlSolid;
Microsoft.Office.Interop.Excel.Range c3 = (Microsoft.Office.Interop.Excel.Range)oSheet.Cells[rowEnd, columnStart];
Microsoft.Office.Interop.Excel.Range c4 = oSheet.get_Range(c1, c3);
oSheet.get_Range(c3, c4).HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
oExcel.Visible = true;
}
}
You need to use field Microsoft.Office.Interop.Excel.Application oExcel in your class to store its value. First time it will be null, but on second - it will be opened excel. But you must be carefull with such a behavior, user can close excel, before 2nd export. So you need implement Closed event handler and clear your field (similar problem)
I want to export data from datagrid to excel file with below code I am exporting the data to excel but Iwant excel file of readonly which I am not getting with below code:
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = true;
worksheet = workbook.ActiveSheet;
for (int i = 1; i < dg1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dg1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dg1.Rows.Count - 1; i++)
{
for (int j = 0; j < dg1.Columns.Count; j++)
{
if (dg1.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = dg1.Rows[i].Cells[j].Value.ToString();
}
else
{
worksheet.Cells[i + 2, j + 1] = "";
}
}
worksheet.Columns.AutoFit();
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Missing.Value);
Microsoft.Office.Interop.Excel._Worksheet worksheet = workbook.ActiveSheet;
worksheet = workbook.ActiveSheet;
//app.Visible = true;
//getting all the columns from datagrid
for (int i = 1; i < dg1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dg1.Columns[i - 1].HeaderText;
}
//getting row collection
for (int i = 0; i < dg1.Rows.Count - 1; i++)
{
for (int j = 0; j < dg1.Columns.Count; j++)
{
if (dg1.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = dg1.Rows[i].Cells[j].Value.ToString();
}
else
{
worksheet.Cells[i + 2, j + 1] = "";
}
worksheet.Columns.AutoFit();
}
}
//to make the excel sheet readonly
((Excel.Worksheet)app.ActiveWorkbook.Sheets[1]).EnableSelection = Excel.XlEnableSelection.xlUnlockedCells;
((Excel.Worksheet)app.ActiveWorkbook.Sheets[1]).Protect(Password: protectionpasswprd, AllowFormattingCells: false);
app.ActiveWorkbook.SaveCopyAs("D:\\a.xls");
app.ActiveWorkbook.Saved = true;
app.Quit();