My problem is I want to convert my time in DataGridView into 12 hours format while I export it into Excel file.
This is my 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;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Exported from gridview";
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
(worksheet.Rows[i + 2 + ":" + i + 2, System.Reflection.Missing.Value] as Microsoft.Office.Interop.Excel.Range).NumberFormat = "#";
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
worksheet.Cells[i + 2, 3].NumberFormat = "h:mm AM/PM";
}
}
var saveFileDialoge = new SaveFileDialog();
saveFileDialoge.FileName = "output";
saveFileDialoge.DefaultExt = ".xlsx";
if (saveFileDialoge.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(saveFileDialoge.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Visible = true;
This is if I debug my codes:
This is if I export it in excel:
Related
if i export the time is null in the datagridview then when i export it the time null is become 12:00 i dont know how to condition it in exporting excel file.
this is my 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;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Exported from gridview";
for (int i = 1; i < dataGridViewIn.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridViewIn.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridViewIn.Rows.Count - 1; i++)
{
(worksheet.Rows[i + 2 + ":" + i + 2, System.Reflection.Missing.Value] as Microsoft.Office.Interop.Excel.Range).NumberFormat = "#";
for (int j = 0; j < dataGridViewIn.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridViewIn.Rows[i].Cells[j].Value.ToString();
worksheet.Cells[i + 2, 3].NumberFormat = "m/d/yy h:mm AM/PM";
}
}
var saveFileDialoge = new SaveFileDialog();
saveFileDialoge.FileName = "TimeIn";
saveFileDialoge.DefaultExt = ".xlsx";
if (saveFileDialoge.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(saveFileDialoge.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Visible = true;
}
Just add a check and write into the excel cell only if there is data to write.
for (int j = 0; j < dataGridViewIn.Columns.Count; j++)
{
if (dataGridViewIn.Rows[i].Cells[j].Value != null)
{
worksheet.Cells[i + 2, j + 1] = dataGridViewIn.Rows[i].Cells[j].Value.ToString();
worksheet.Cells[i + 2, 3].NumberFormat = "m/d/yy h:mm AM/PM";
}
}
While exporting the datagridview to Excel in C#.net ,leading zeros are missing.
For example : Data in the cell is "001693"
After exporting to excel it is displaying me like this "1693".
Please help me out of this.
this is my 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;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "Exported from gridview";
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
var saveFileDialoge = new SaveFileDialog();
saveFileDialoge.FileName = "output";
saveFileDialoge.DefaultExt = ".xlsx";
if (saveFileDialoge.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(saveFileDialoge.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Visible = true;
You need to set text format to cell where you want to save number with leading zeroes.
In this sample I add code which set text format for entire row to speed up processing.
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
(worksheet.Rows[i+2 + ":" + i+2, System.Reflection.Missing.Value] as Excel.Range).NumberFormat = "#";
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
When I export grid view data to export to the Excel file, I get error null value. Please help me correct my codes.
object mis = System.Reflection.Missing.Value;
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 = false;
worksheet = (Worksheet)workbook.Sheets["Sheet1"];
worksheet = (Worksheet)workbook.ActiveSheet;
worksheet.Name = "Export";
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Document(*.xlsx)|*.xlsx";
sfd.FileName = "Export";
if (sfd.ShowDialog() == DialogResult.OK)
{
workbook.SaveAs(sfd.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
app.Quit();
var app = new Application();
_Workbook workbook = app.Workbooks.Add(Type.Missing);
app.Visible = true;
_Worksheet worksheet = (Worksheet) workbook.Sheets["Sayfa1"];
worksheet.Name = "Export";
dataGridView1.Columns[0].Visible = false;
List < DataGridViewColumn > listVisible = new List < DataGridViewColumn > ();
foreach(DataGridViewColumn col in dataGridView1.Columns) {
if (col.Visible)
listVisible.Add(col);
}
for (int i = 0; i < listVisible.Count; i++) {
worksheet.Cells[1, i + 1] = listVisible[i].HeaderText;
}
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) {
for (int j = 0; j < listVisible.Count; j++) {
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[listVisible[j].Name].Value.ToString();
}
}
using Microsoft.Office.Interop.Excel;
using Application = Microsoft.Office.Interop.Excel.Application;
var app = new Application();
_Workbook workbook = app.Workbooks.Add(Type.Missing);
app.Visible = false;
_Worksheet worksheet = (Worksheet) workbook.Sheets["Sayfa1"];
worksheet.Name = "Export";
for (var i = 1; i < dataGridView1.Columns.Count + 1; i++)
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
for (var i = 0; i < dataGridView1.Rows.Count - 1; i++)
for (var j = 0; j < dataGridView1.Columns.Count; j++)
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value;
var sfd = new SaveFileDialog();
sfd.Filter = "Excel Document(*.xlsx)|*.xlsx";
sfd.FileName = "Export";
if (sfd.ShowDialog() == DialogResult.OK)
workbook.SaveAs(sfd.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
app.Quit();
I know this topic has been discussed but I think it has some differences. I have dates stored in my database. So when I export data table into EXCEL, it shows like the image.
Here is the image of the EXCEL file:
I need to add only date. Not HH:MM:SS included. my code is pasted below:
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;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
worksheet.Name = "InternDetails";
for (int i = 1; i < dataGridView3.Columns.Count + 1; i++){
worksheet.Cells[1, i] = dataGridView3.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView3.Rows.Count; i++){
for (int j = 0; j < dataGridView3.Columns.Count; j++){
worksheet.Cells[i + 2, j + 1] = dataGridView3.Rows[i].Cells[j].Value.ToString();
}
}
var saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = "Interns";
saveFileDialog.DefaultExt = ".xlsx";
if (saveFileDialog.ShowDialog() == DialogResult.OK){
workbook.SaveAs(saveFileDialog.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
}
Thanks for the help.
Ok,if you want to show date only.and no time,you should set the number format.
just look at the code like this:
SomeCell.NumberFormat="yyyy-mm-dd"
it will show for example:"2018-05-20"
Correct me if I'm wrong, for what I saw in the provided screenshot, it consist of 3 date column which is statusdate, fplacementdate and periodcomplertion.
Have you tried it this way?
First create a conversion function for you date.
public static string convertDateFormat(this string date)
{
DateTime dateFormat = Convert.ToDateTime(date);
if (dateFormat != DateTime.MinValue)
{
return String.Format("{0:MM/dd/yyyy}", dateFormat);
}
else
{
return "";
}
}
Then use it this way
for (int i = 0; i < dataGridView3.Rows.Count; i++){
for (int j = 0; j < dataGridView3.Columns.Count; j++){
string formatValue = dataGridView3.Rows[i].Cells[j].Value.ToString();
if( dataGridView3.Columns[j].HeaderText == "statusdate" || dataGridView3.Columns[j].HeaderText == "fplacementdate" || dataGridView3.Columns[j].HeaderText == "periodcomplertion"){
formatValue.convertDateFormat();
}
worksheet.Cells[i + 2, j + 1] = formatValue;
}
}
I export all of datagrid data to Excel workbook sheet (Sheet1) using these code blocks:
private void copyAlltoClipboard()
{
dataGridView1.SelectAll();
DataObject dataObj = dataGridView1.GetClipboardContent();
if (dataObj != null)
Clipboard.SetDataObject(dataObj);
}
private void button3_Click_1(object sender, EventArgs e)
{
copyAlltoClipboard();
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlexcel = new Excel.Application();
xlexcel.Visible = true;
xlWorkBook = xlexcel.Workbooks.Open("C:\\Test.xls", Type.Missing, true, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
CR.Select();
xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);
}
But, I want to export some datagrid rows to another sheet (Sheet2), and remaining rows to another sheet (Sheet3). So, I should separate datagrid data to X pieces. And Excel worksheets count should be the same X and should contain some specific datagrid data. How can I do this?
This is how I do it,
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet1;// defines sheet1
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet2;// defines sheet2
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet3;// defines sheet3
object misValue = System.Reflection.Missing.Value;
xlWorkBook = xlexcel.Workbooks.Add(misValue);
for (int i = 0; i < 2; i++) // here we add the rest of sheet into the excel
{
xlexcel.Sheets.Add(After: xlexcel.Sheets[xlexcel.Sheets.Count]);
}
xlWorkSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); //setting the first sheet equal to first sheet in excel
xlWorkSheet2 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2);//setting the 2nd sheet equal to first sheet in excel
xlWorkSheet3 = (Microsoft.Office.Interop.Excel.Worksheet)xlWorkBook.Worksheets.get_Item(3);
//here we add the data to the sheet from datagridview
for (int j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
xlWorkSheet1.Cells[1, j + 1] = dataGridView1.Columns[j].HeaderText;
}
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
for (int j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
xlWorkSheet1.Cells[i + 2, j + 1] = cell.Value;
}
}
xlWorkSheet1.Name = "put name here";
xlWorkSheet1.Columns.AutoFit();
for (int j = 0; j <= dataGridView2.ColumnCount - 1; j++)
{
xlWorkSheet2.Cells[1, j + 1] = dataGridView2.Columns[j].HeaderText;
}
for (int i = 0; i <= dataGridView2.RowCount - 1; i++)
{
for (int j = 0; j <= dataGridView2.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView2[j, i];
xlWorkSheet2.Cells[i + 2, j + 1] = cell.Value;
}
}
xlWorkSheet2.Name = "put name here";
xlWorkSheet2.Columns.AutoFit();
for (int j = 0; j <= dataGridView3.ColumnCount - 1; j++)
{
xlWorkSheet3.Cells[1, j + 1] = dataGridView3.Columns[j].HeaderText;
}
for (int i = 0; i <= dataGridView3.RowCount - 1; i++)
{
for (int j = 0; j <= dataGridView3.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView3[j, i];
xlWorkSheet3.Cells[i + 2, j + 1] = cell.Value;
}
}
xlWorkSheet3.Name = "put name here";
xlWorkSheet3.Columns.AutoFit();
xlWorkBook.SaveAs("put path here", Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlexcel.Quit();