Fork me on Gitee

Unity C# 运用 GetSaveFileName() 导出Excel文件

本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6944870.html

唉哟,这次厉害咯,网上搜罗了好久,终于被我找到汉化的保存对话框了,根据网上的一些前辈总结的内容,做了一些修改,

先放个效果图:

 

首先需要定义一个OpenFileName的类:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

public class OpenFileName
{
	public int structSize = 0;
	public IntPtr dlgOwner = IntPtr.Zero;
	public IntPtr instance = IntPtr.Zero;
	public String filter = null;
	public String customFilter = null;
	public int maxCustFilter = 0;
	public int filterIndex = 0;
	public String file = null;
	public int maxFile = 0;
	public String fileTitle = null;
	public int maxFileTitle = 0;
	public String initialDir = null;
	public String title = null;
	public int flags = 0;
	public short fileOffset = 0;
	public short fileExtension = 0;
	public String defExt = null;
	public IntPtr custData = IntPtr.Zero;
	public IntPtr hook = IntPtr.Zero;
	public String templateName = null;
	public IntPtr reservedPtr = IntPtr.Zero;
	public int reservedInt = 0;
	public int flagsEx = 0;
}

  当然,也不是都用到了,只用到了一小部分,有性趣的童鞋可以个性化一下保存对话框,

然后是最重要的委托GetSaveName()方法:

public class DllTest
{
	[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
	public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
}

  挡挡挡,最后调用一下即可:

public static void OpenDialog(Action<Stream> onSave)
    {
        OpenFileName ofn = new OpenFileName();
        ofn.structSize = Marshal.SizeOf(ofn);
    
        ofn.filter = "Excel (*.xls)\0*.xls\0\0";
        ofn.file = new string(new char[256]);
        ofn.maxFile = ofn.file.Length;
        ofn.fileTitle = new string(new char[64]);
        ofn.maxFileTitle = ofn.fileTitle.Length;
        ofn.initialDir = UnityEngine.Application.dataPath;//默认路径
        ofn.title = "保存文件";
        ofn.defExt = ".xls";//显示文件的类型
        ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        if (DllTest.GetSaveFileName(ofn))
        {
            dosomething();

            string Savepath = Path.GetDirectoryName (ofn.file);
            Process.Start (Savepath);
        }
    }

 

 

终于搞定保存对话框汉化了,5555,也是不容易啊!

那位小伙伴有更好的方法希望可以分享给我啊!

 

2017/12/27 

前两天同时使用的时候遇到很多问题,原因是封装的不太好,而且也没有注释,逻辑提供不全,今天把完整的代码贴上来,最近比较忙,等空闲了再整理下

首先是调用OpenDialog的方法

void save()
	{
		SaveDialog.OpenDialog (saveExcel);
	}

	public void saveExcel(Stream s)
	{
//excell的逻辑
		ExcelWrite ew = new ExcelWrite ();
		ew.WriteToStream (s);
	}

  然后是 ExcelWrite.cs

public void WriteToStream(Stream s)
	{
		IWorkbook workbook = new HSSFWorkbook ();
		ISheet sheet = workbook.CreateSheet ();

		IRow row = sheet.CreateRow (0);//参数0表示第0行

		string[] firstRow = new string[] 
		{
			"ID",
			"性别",
			"博客"
		};

		for (int i = 0; i < firstRow.Length; i++) 
		{
			ICell cell = row.CreateCell (i);
			cell.SetCellValue (firstRow [i]);
		}

		IRow row2 = sheet.CreateRow (1);

		string[] secondRow = new string[] 
		{ 
			"AdvancePikachu",
			"男",
			"http://www.cnblogs.com/AdvancePikachu/"
		};

		for (int i = 0; i < secondRow.Length; i++)
		{
			ICell cell = row2.CreateCell (i);
			cell.SetCellValue (secondRow [i]);
		}

		workbook.Write (s);
	}

  这个excel用的是NPOI写的,效果挺好的,最后附上demo实例,欢迎各路大神提供更好的思路!

Test
posted @ 2017-06-05 14:43  AdvancePikachu  阅读(914)  评论(0编辑  收藏  举报