مشکل قطعه کد زیر چیست که ارور میدهد ( خروجی دیتا گرید ب اکسل )
سلام کد زیر جهت خروجی دیتا گرید به اکسل هست ولی متاسفانه فقط سطر اول وارد خروجی اکسل میکنه واز قطه کد زیر ارور میگیره
// creating Excel Application
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
// creating new WorkBook within Excel application
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
// creating new Excelsheet in workbook
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
// see the excel sheet behind the program
app.Visible = true;
// get the reference of first sheet. By default its name is Sheet1.
// store its reference to worksheet
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
// changing the name of active sheet
worksheet.Name = "Exported from gridview";
// storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
// storing Each row and column value to excel sheet
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();
}
}
// save the application
// Exit from the application
app.Quit();
قطعه کدی ک ارور میگیره ازش
worksheet.Cells[i +2, j+1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
نقل قول: مشکل قطعه کد زیر چیست که ارور میدهد ( خروجی دیتا گرید ب اکسل )
از این کد استفاده کن
private void ToCsV(DataGridView dGV, string filename)
{
string stOutput = "";
// Export titles:
string sHeaders = "";
for (int j = 0; j < dGV.Columns.Count; j++)
sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
stOutput += sHeaders + "\r\n";
// Export data.
for (int i = 0; i < dGV.RowCount - 1; i++)
{
string stLine = "";
for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
stOutput += stLine + "\r\n";
}
Encoding utf16 = Encoding.GetEncoding(1254);
UTF8Encoding utf8 = new UTF8Encoding();
byte[] output = utf8.GetBytes(stOutput);
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(output, 0, output.Length); //write the encoded file
bw.Flush();
bw.Close();
fs.Close();
}
نقل قول: مشکل قطعه کد زیر چیست که ارور میدهد ( خروجی دیتا گرید ب اکسل )
برای رفع مشکل فارسی ک بصورت ؟؟؟
ذخیره میکند باید چی کار کرد
و همچنین راست ب چپ بودن فایل اکسلمون
کد کامل دیتا گرید ب اکسل ب این صورت است
private void ToCsV(DataGridView dGV, string filename)
{
string stOutput = "";
// Export titles:
string sHeaders = "";
for (int j = 0; j < dGV.Columns.Count; j++)
sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
stOutput += sHeaders + "\r\n";
// Export data.
for (int i = 0; i < dGV.RowCount - 1; i++)
{
string stLine = "";
for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
stOutput += stLine + "\r\n";
}
Encoding utf16 = Encoding.GetEncoding(1254);
byte[] output = utf16.GetBytes(stOutput);
FileStream fs = new FileStream(filename, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(output, 0, output.Length); //write the encoded file
bw.Flush();
bw.Close();
fs.Close();
}
و برای دگمه
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Documents (*.xls)|*.xls";
sfd.FileName = "export.xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
//ToCsV(dataGridView1, @"c:\export.xls");
ToCsV(dataGridView1, sfd.FileName); // Here dataGridview1 is your grid view name
}
نقل قول: مشکل قطعه کد زیر چیست که ارور میدهد ( خروجی دیتا گرید ب اکسل )
در ضمن یکی از جدول هام رو میتونه کامل نشون بده ولی اون یکی جدول فقط سطر اولش تبدیل ب اکسل میکنه
مشکل
؟؟؟؟؟؟ و راست ب چپ هنوز پا بر جاست ..... کدی قبلی ک داشتم همین کار میکرد
نقل قول: مشکل قطعه کد زیر چیست که ارور میدهد ( خروجی دیتا گرید ب اکسل )
برای مشگل نمایش فارسی Encoding.GetEncoding رو default بزار
نقل قول: مشکل قطعه کد زیر چیست که ارور میدهد ( خروجی دیتا گرید ب اکسل )
Encoding utf16 = Encoding.GetEncoding(1254);
برای فارسی کردن کافی 1256 بشه
نقل قول: مشکل قطعه کد زیر چیست که ارور میدهد ( خروجی دیتا گرید ب اکسل )
مشکل اصلی اینجاست سطر ها رو کامل نشون نمیده یعنی همه رو ب خروجی میبره الی سطر اخر هر جدول رو