Export Excel file (for dev)

Source: Internet
Author: User

File class:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.IO;
Using System.Windows.Forms;

Namespace Datahandle_print_export_
{
Class Fileprocess
{
Private fileprocess () {}
public static readonly fileprocess Instance = new fileprocess ();

<summary>
Description: Open the file prompt box
</summary>
<param name= "OpenType" > Open file Type </param>
<returns> Open Path </returns>
public string OpenDialog (string openType)
{
OpenFileDialog OpenFileDialog = new OpenFileDialog ();
Openfiledialog.filter = OpenType;
Openfiledialog.restoredirectory = true;
Openfiledialog.filterindex = 1;
if (openfiledialog.showdialog () = = DialogResult.OK)
{
return openfiledialog.filename;
}
Return "";
}

<summary>
Description: Save file hint box (incoming suffix returns save address)
</summary>
<param name= "ext" > Save extension </param>
<returns> return to save path </returns>
public string Savedialog (string ext)
{
String Localfilepath = "", Filenameext = "", NewFileName = "", FilePath = "";
SaveFileDialog SaveFileDialog = new SaveFileDialog ();

Set File type
Writing rules such as: TXT files (*.txt) |*.txt
Savefiledialog.filter = string. Format ("{0} File (*.{ 0}) |*. {0}| All Files (*. *) |*.* ");
Set default file name (can not be set)
Savefiledialog.filename = DateTime.Now.ToString ("YYYYMMDDHHMMSS") + "." + ext;
Gets or sets a value that indicates whether the dialog box automatically adds an extension to the file name if the user omits the extension.
Savefiledialog.addextension = true;
Save dialog box remembers the last Open Directory
Savefiledialog.restoredirectory = true;

DialogResult result = Savefiledialog.showdialog ();

if (result = = DialogResult.OK)
{
Get file path
Localfilepath = SaveFileDialog.FileName.ToString ();

return localfilepath;
}
Return "";
}

<summary>
Description: File stream Save
</summary>
<param name= "Stream" > file stream to be saved </param>
<param name= "path" > File path </param>
public void Filesteamsave (stream stream, string path)
{
Stream sourcestream = stream;
FileStream targetstream = null;
using (Targetstream = new FileStream (path, FileMode.Create, FileAccess.Write, Fileshare.none))
{
const int bufferlen = 4096;
byte[] buffer = new Byte[bufferlen];
int count = 0;
while ((count = sourcestream.read (buffer, 0, Bufferlen)) > 0)
{
Targetstream.write (buffer, 0, count);
}
Targetstream.close ();
Sourcestream.close ();
}
}

public void Exploreexcel (DevExpress.XtraGrid.Views.Grid.GridView GridView, string fileName)
{
bool Isexport = true;
SaveFileDialog saveFile = new SaveFileDialog ();
Savefile.title = "Please select file path";
Savefile.filter = "Excel document (*.xls) |*.xls| Excel document (*.xlsx) |*.xlsx ";
Savefile.filename = FileName;
if (savefile.showdialog () = = DialogResult.OK)
{
DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions ();
Options. Textexportmode = DevExpress.XtraPrinting.TextExportMode.Text;
Options. SheetName = FileName;
GridView.OptionsPrint.AutoWidth = false;
GridView.AppearancePrint.Row.Font = new System.Drawing.Font ("Arial", 9);
Try
{
Gridview.exporttoxls (savefile.filename, Options);
}
catch (Exception ex)
{
MessageBox.Show ("File is open, in use, export failed", "Prompt", Messageboxbuttons.okcancel, MessageBoxIcon.Information, Messageboxdefaultbutton.button2);
Isexport = false;
}

if (Isexport)
{
if (MessageBox.Show ("Export succeeded, do you want to open the file?") "," inquiry ", Messageboxbuttons.yesno, messageboxicon.question) = = Dialogresult.yes)
System.Diagnostics.Process.Start (Savefile.filename);

}
}
}
}
}

Client Calls

FileProcess.Instance.ExploreExcel (GridView1, "Test" + DateTime.Now.ToString ("Yyyymmddhhmmss"));

Export Excel file (for dev)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.