PDA

View Full Version : سوال: خروجی های مختلف از List View



hackman
جمعه 03 خرداد 1392, 02:54 صبح
سلام

چه جوری میتونم از یه لیست ویو خروجی اکسل بگیرم

یا به pdf تبدیلش کنم ...

هر دو مورد رو لازم دارم لطفا راهنمایی کنید

hackman
جمعه 03 خرداد 1392, 19:55 عصر
کوووووممممممممممککککککککک ککک

khokhan
جمعه 03 خرداد 1392, 20:47 عصر
کوووووممممممممممککککککککک ککک
سلام

برای انتقال محتوای گرید به ایکسل :

بعد از اینکه Interop.Office و یه چند تای دیگه که در تصویر اومده رو به رفرنس اضافه کردین در رویداد کلیک یک با تن اینطور می نویسین :





private void btn_export_Click(object sender, EventArgs e)
{

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;

try
{

worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook .Sheets["Sheet1"];
worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook .ActiveSheet;
worksheet.Name = "Exported from DataGridView";

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();
}
}


string fileName = String.Empty;

saveFileExcel.Filter = "Excel files |*.xls|All files (*.*)|*.*";
saveFileExcel.FilterIndex = 2;
saveFileExcel.RestoreDirectory = true;

if (saveFileExcel.ShowDialog() == DialogResult.OK)
{
fileName = saveFileExcel.FileName;

workbook.SaveAs(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);

}
else
return;

}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
app.Quit();
workbook = null;
app = null;
}
}

khokhan
جمعه 03 خرداد 1392, 20:51 عصر
این هم برای listview :

Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();

xla.Visible = true;

Microsoft.Office.Interop.Excel.Workbook wb = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.X lSheetType.xlWorksheet);

Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)xla.Acti veSheet;

int i = 1;

int j = 1;

foreach (ListViewItem comp in listView1.Items)

{

ws.Cells[i, j] = comp.Text.ToString();

//MessageBox.Show(comp.Text.ToString());

foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)

{

ws.Cells[i, j] = drv.Text.ToString();

j++;

}

j = 1;

i++;

}

hackman
سه شنبه 07 خرداد 1392, 15:38 عصر
این هم برای listview :

Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();

xla.Visible = true;

Microsoft.Office.Interop.Excel.Workbook wb = xla.Workbooks.Add(Microsoft.Office.Interop.Excel.X lSheetType.xlWorksheet);

Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)xla.Acti veSheet;

int i = 1;

int j = 1;

foreach (ListViewItem comp in listView1.Items)

{

ws.Cells[i, j] = comp.Text.ToString();

//MessageBox.Show(comp.Text.ToString());

foreach (ListViewItem.ListViewSubItem drv in comp.SubItems)

{

ws.Cells[i, j] = drv.Text.ToString();

j++;

}

j = 1;

i++;

}

اینو کجا ذخیره میکنه ؟

khokhan
سه شنبه 07 خرداد 1392, 15:44 عصر
اینو کجا ذخیره میکنه ؟

یا بذارش توی یه متد و در رویداد کلیک باتن متد رو فراخوانی کن

یا اینکه مستقیما داخل رویداد باتن این کد رو قرار بده

hackman
سه شنبه 07 خرداد 1392, 15:57 عصر
یا بذارش توی یه متد و در رویداد کلیک باتن متد رو فراخوانی کن

یا اینکه مستقیما داخل رویداد باتن این کد رو قرار بده

منظورم اینه : فایل اکسلی که ساخته میشه کجا سیو میشه ؟

hackman
سه شنبه 07 خرداد 1392, 15:59 عصر
pdf چه جوریه ؟؟؟

من این کدو برا pdf پیدا کردم اما کار نمیکنه :


oleDbConnection1.Open();
this.cellExport1.ActionAfterExport = DataExport.Common.ActionType.None;
this.cellExport1.DataFormats.CultureName = "zh-CN";
this.cellExport1.DataFormats.Currency = "?#,###,##0.00";
this.cellExport1.DataFormats.DateTime = "yyyy-M-d H:mm";
this.cellExport1.DataFormats.Float = "#,###,##0.00";
this.cellExport1.DataFormats.Integer = "#,###,##0";
this.cellExport1.DataFormats.Time = "H:mm";
this.cellExport1.SheetOptions.AggregateFormat.Font .Name = "Arial";
this.cellExport1.SheetOptions.CustomDataFormat.Fon t.Name = "Arial";
this.cellExport1.SheetOptions.DefaultFont.Name = "Arial";
this.cellExport1.SheetOptions.FooterFormat.Font.Na me = "Arial";
this.cellExport1.SheetOptions.HeaderFormat.Font.Na me = "Arial";
this.cellExport1.SheetOptions.HyperlinkFormat.Font .Color = Spire.DataExport.XLS.CellColor.Blue;
this.cellExport1.SheetOptions.HyperlinkFormat.Font .Name = "Arial";
this.cellExport1.SheetOptions.HyperlinkFormat.Font .Underline = Spire.DataExport.XLS.XlsFontUnderline.Single;
this.cellExport1.SheetOptions.NoteFormat.Alignment .Horizontal = Spire.DataExport.XLS.HorizontalAlignment.Left;
this.cellExport1.SheetOptions.NoteFormat.Alignment .Vertical = Spire.DataExport.XLS.VerticalAlignment.Top;
this.cellExport1.SheetOptions.NoteFormat.Font.Bold = true;
this.cellExport1.SheetOptions.NoteFormat.Font.Name = "Tahoma";
this.cellExport1.SheetOptions.NoteFormat.Font.Size = 8F;
this.cellExport1.SheetOptions.TitlesFormat.Font.Bo ld = true;
this.cellExport1.SheetOptions.TitlesFormat.Font.Na me = "Arial";
this.cellExport1.DataSource = DataExport.Common.ExportSource.ListView;
this.cellExport1.ListView = this.listView1;

Using (MemoryStream stream = new MemoryStream())
{
cellExport1.SaveToFile(stream);
this.oleDbConnection1.Close();
Workbook workbook = new Workbook(stream);
PdfConverter pdfConverter = new PdfConverter(workbook);

PdfDocument pdfDocument = new PdfDocument();
pdfDocument.PageSettings.Orientation = pdf.PdfPageOrientation.Landscape;
pdfDocument.PageSettings.Width = 970;
pdfDocument.PageSettings.Height = 850;

PdfConverterSettings settings = new PdfConverterSettings();
settings.TemplateDocument = pdfDocument;
pdfDocument = pdfConverter.Convert(settings);

pdfDocument.SaveToFile("test.pdf");

}

khokhan
سه شنبه 07 خرداد 1392, 16:57 عصر
منظورم اینه : فایل اکسلی که ساخته میشه کجا سیو میشه ؟

با فشار دادن باتن حاوی این کد یه فایل اکسل وا می شه و محتویات لیست داخلش ریخته می شه حالا اختیار با شماست فایل اکسل رو هرکجا خواستین ذخیره کنین

khokhan
سه شنبه 07 خرداد 1392, 20:18 عصر
pdf چه جوریه ؟؟؟

من این کدو برا pdf پیدا کردم اما کار نمیکنه :


oleDbConnection1.Open();
this.cellExport1.ActionAfterExport = DataExport.Common.ActionType.None;
this.cellExport1.DataFormats.CultureName = "zh-CN";
this.cellExport1.DataFormats.Currency = "?#,###,##0.00";
this.cellExport1.DataFormats.DateTime = "yyyy-M-d H:mm";
this.cellExport1.DataFormats.Float = "#,###,##0.00";
this.cellExport1.DataFormats.Integer = "#,###,##0";
this.cellExport1.DataFormats.Time = "H:mm";
this.cellExport1.SheetOptions.AggregateFormat.Font .Name = "Arial";
this.cellExport1.SheetOptions.CustomDataFormat.Fon t.Name = "Arial";
this.cellExport1.SheetOptions.DefaultFont.Name = "Arial";
this.cellExport1.SheetOptions.FooterFormat.Font.Na me = "Arial";
this.cellExport1.SheetOptions.HeaderFormat.Font.Na me = "Arial";
this.cellExport1.SheetOptions.HyperlinkFormat.Font .Color = Spire.DataExport.XLS.CellColor.Blue;
this.cellExport1.SheetOptions.HyperlinkFormat.Font .Name = "Arial";
this.cellExport1.SheetOptions.HyperlinkFormat.Font .Underline = Spire.DataExport.XLS.XlsFontUnderline.Single;
this.cellExport1.SheetOptions.NoteFormat.Alignment .Horizontal = Spire.DataExport.XLS.HorizontalAlignment.Left;
this.cellExport1.SheetOptions.NoteFormat.Alignment .Vertical = Spire.DataExport.XLS.VerticalAlignment.Top;
this.cellExport1.SheetOptions.NoteFormat.Font.Bold = true;
this.cellExport1.SheetOptions.NoteFormat.Font.Name = "Tahoma";
this.cellExport1.SheetOptions.NoteFormat.Font.Size = 8F;
this.cellExport1.SheetOptions.TitlesFormat.Font.Bo ld = true;
this.cellExport1.SheetOptions.TitlesFormat.Font.Na me = "Arial";
this.cellExport1.DataSource = DataExport.Common.ExportSource.ListView;
this.cellExport1.ListView = this.listView1;

Using (MemoryStream stream = new MemoryStream())
{
cellExport1.SaveToFile(stream);
this.oleDbConnection1.Close();
Workbook workbook = new Workbook(stream);
PdfConverter pdfConverter = new PdfConverter(workbook);

PdfDocument pdfDocument = new PdfDocument();
pdfDocument.PageSettings.Orientation = pdf.PdfPageOrientation.Landscape;
pdfDocument.PageSettings.Width = 970;
pdfDocument.PageSettings.Height = 850;

PdfConverterSettings settings = new PdfConverterSettings();
settings.TemplateDocument = pdfDocument;
pdfDocument = pdfConverter.Convert(settings);

pdfDocument.SaveToFile("test.pdf");

}


بایستی کتابخانه itextsharp.dll رو توی رفرنس پروژه اضافه کنی و همچنین در قسمت using هم این دو خط رو اضافه کنی


using iTextSharp.text;
using iTextSharp.text.pdf;

من کتابخانه مذکور رو با یه نمونه کامل از استفاده این کتابخانه براتون ضمیمه می کنم :لبخند: در ضمن نحوه استفاده از این کتابخانه بصورت فایل pdf داخل نمونه به نمایش در می آید

نمونه روهمراه با کتابخانه از این لینک دریافت کنید http://uplod.ir/y4qpfgxbahlb/iiii.rar.htm