C#如何通过直接写文件生成xls的Excel文件

时间:2022-05-15 13:32:22
rt ,不能通过api打开office excel然后逐单元格的去写~而是需要通过直接写文件来完成。如果用直接写文本文件用tab隔开的话在excel中打开会有警告~~

9 个解决方案

#1


        /// <summary>
        /// 名称:WriteToExcel
        /// 功能:查询结果导出Execl
        /// 编写人:
        /// 编写时间:2008-11-26
        /// </summary>
        /// <param name="table"></param>
        public void WriteToExcel(DataTable table)
        {
            try
            {
                string tempImagePath = Application.StartupPath;
                string temp = tempImagePath + "\\ExeclFiles";
                Directory.CreateDirectory(@temp);
                string strFilePath = @Application.StartupPath + @"\ExeclFiles\" + CommonClass.CommonDateTime() + ".xls";
                System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath,true, System.Text.Encoding.Default);
                object[] values = new object[table.Columns.Count];
                sw.Write("\r\n");
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    for (int j = 0; j < values.Length; ++j)
                    {
                        sw.Write(table.Rows[i][j].ToString());
                        sw.Write('\t');
                    }
                    sw.Write("\r\n");
                }
                sw.Flush();
                sw.Close();
                CommonClass.MessageBoxOK("成功导出[" + ds.Tables[0].Rows.Count.ToString() + "]行到Execl!");
            }
            catch
            {
                CommonClass.MessageBoxNo("导出Execl失败!");
            }
        }

#2


楼上的是正解

#3


http://blog.csdn.net/43720938/archive/2007/04/24/1577212.aspx
可参考

#4


可以,Up!

#5


#6


C# code///<summary>/// 名称:WriteToExcel/// 功能:查询结果导出Execl/// 编写人:/// 编写时间:2008-11-26///</summary>///<param name="table"></param>publicvoid WriteToExcel(DataTable table)
        {try
            {string¡­
[/Quote]

#7


libinguest 你的方法就是等于写文本文件用,隔开吧~~我这里有如下代码,但是只支持16位的长度到35767
 public ExcelWriter(string strPath) 
         { 
             _wirter = new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate); 
         } 
        /// <summary> 
        /// 写入short数组 
        /// </summary> 
        /// <param name="values"></param> 
        private void _writeFile(short[] values) 
         { 
            foreach (short v in values) 
             { 
                byte[] b = System.BitConverter.GetBytes(v); 
                 _wirter.Write(b, 0, b.Length); 
             } 
         } 
        /// <summary> 
        /// 写文件头 
        /// </summary> 
        public void BeginWrite() 
         { 
             _writeFile(new short[] { 0x809, 8, 0, 0x10, 0, 0 }); 
         } 
        /// <summary> 
        /// 写文件尾 
        /// </summary> 
        public void EndWrite() 
         { 
             _writeFile(new short[] { 0xa, 0 }); 
             _wirter.Close(); 
         } 
        /// <summary> 
        /// 写一个数字到单元格x,y 
        /// </summary> 
        /// <param name="x"></param> 
        /// <param name="y"></param> 
        /// <param name="value"></param> 
        public void WriteNumber(short x, short y, double value) 
         { 
             _writeFile(new short[] { 0x203, 14, x, y, 0 }); 
            byte[] b = System.BitConverter.GetBytes(value); 
             _wirter.Write(b, 0, b.Length); 
         } 
        /// <summary> 
        /// 写一个字符到单元格x,y 
        /// </summary> 
        /// <param name="x"></param> 
        /// <param name="y"></param> 
        /// <param name="value"></param> 
        public void WriteString(short x, short y, string value) 
         { 
            byte[] b = System.Text.Encoding.Default.GetBytes(value); 
             _writeFile(new short[] { 0x204, (short)(b.Length + 8), x, y,0, (short)b.Length }); 
             _wirter.Write(b, 0, b.Length); 
         }

#8


public static bool  ExportExcel(string strFileName, GridView girdview,bool isHead)
        {
            string strExcelFileName = strFileName;

            FileStream fs = new FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write);

            StreamWriter w = new StreamWriter(fs, Encoding.Unicode);
            try
            {
                if (isHead)
                {
                    for (int i = 0; i < girdview.Columns.Count; i++)
                    {
                        w.Write(girdview.Columns[i].HeaderText.Trim() + Convert.ToChar(9));
                    }
                    w.Write("\n");
                }

                for (int i = 0; i < girdview.Rows.Count; i++)
                {
                    for (int j = 0; j < girdview.Columns.Count; j++)
                    {
                        w.Write(girdview.Rows[i].Cells[j].Text.Trim() + Convert.ToChar(9));
                    }
                    w.Write("\n");
                }
                w.Flush();
                w.Close();
            }
            catch
            {
                w.Close();
                return false;
            }
            return true;
        }

#9


好好学习~

#1


        /// <summary>
        /// 名称:WriteToExcel
        /// 功能:查询结果导出Execl
        /// 编写人:
        /// 编写时间:2008-11-26
        /// </summary>
        /// <param name="table"></param>
        public void WriteToExcel(DataTable table)
        {
            try
            {
                string tempImagePath = Application.StartupPath;
                string temp = tempImagePath + "\\ExeclFiles";
                Directory.CreateDirectory(@temp);
                string strFilePath = @Application.StartupPath + @"\ExeclFiles\" + CommonClass.CommonDateTime() + ".xls";
                System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath,true, System.Text.Encoding.Default);
                object[] values = new object[table.Columns.Count];
                sw.Write("\r\n");
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    for (int j = 0; j < values.Length; ++j)
                    {
                        sw.Write(table.Rows[i][j].ToString());
                        sw.Write('\t');
                    }
                    sw.Write("\r\n");
                }
                sw.Flush();
                sw.Close();
                CommonClass.MessageBoxOK("成功导出[" + ds.Tables[0].Rows.Count.ToString() + "]行到Execl!");
            }
            catch
            {
                CommonClass.MessageBoxNo("导出Execl失败!");
            }
        }

#2


楼上的是正解

#3


http://blog.csdn.net/43720938/archive/2007/04/24/1577212.aspx
可参考

#4


可以,Up!

#5


#6


C# code///<summary>/// 名称:WriteToExcel/// 功能:查询结果导出Execl/// 编写人:/// 编写时间:2008-11-26///</summary>///<param name="table"></param>publicvoid WriteToExcel(DataTable table)
        {try
            {string¡­
[/Quote]

#7


libinguest 你的方法就是等于写文本文件用,隔开吧~~我这里有如下代码,但是只支持16位的长度到35767
 public ExcelWriter(string strPath) 
         { 
             _wirter = new System.IO.FileStream(strPath, System.IO.FileMode.OpenOrCreate); 
         } 
        /// <summary> 
        /// 写入short数组 
        /// </summary> 
        /// <param name="values"></param> 
        private void _writeFile(short[] values) 
         { 
            foreach (short v in values) 
             { 
                byte[] b = System.BitConverter.GetBytes(v); 
                 _wirter.Write(b, 0, b.Length); 
             } 
         } 
        /// <summary> 
        /// 写文件头 
        /// </summary> 
        public void BeginWrite() 
         { 
             _writeFile(new short[] { 0x809, 8, 0, 0x10, 0, 0 }); 
         } 
        /// <summary> 
        /// 写文件尾 
        /// </summary> 
        public void EndWrite() 
         { 
             _writeFile(new short[] { 0xa, 0 }); 
             _wirter.Close(); 
         } 
        /// <summary> 
        /// 写一个数字到单元格x,y 
        /// </summary> 
        /// <param name="x"></param> 
        /// <param name="y"></param> 
        /// <param name="value"></param> 
        public void WriteNumber(short x, short y, double value) 
         { 
             _writeFile(new short[] { 0x203, 14, x, y, 0 }); 
            byte[] b = System.BitConverter.GetBytes(value); 
             _wirter.Write(b, 0, b.Length); 
         } 
        /// <summary> 
        /// 写一个字符到单元格x,y 
        /// </summary> 
        /// <param name="x"></param> 
        /// <param name="y"></param> 
        /// <param name="value"></param> 
        public void WriteString(short x, short y, string value) 
         { 
            byte[] b = System.Text.Encoding.Default.GetBytes(value); 
             _writeFile(new short[] { 0x204, (short)(b.Length + 8), x, y,0, (short)b.Length }); 
             _wirter.Write(b, 0, b.Length); 
         }

#8


public static bool  ExportExcel(string strFileName, GridView girdview,bool isHead)
        {
            string strExcelFileName = strFileName;

            FileStream fs = new FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write);

            StreamWriter w = new StreamWriter(fs, Encoding.Unicode);
            try
            {
                if (isHead)
                {
                    for (int i = 0; i < girdview.Columns.Count; i++)
                    {
                        w.Write(girdview.Columns[i].HeaderText.Trim() + Convert.ToChar(9));
                    }
                    w.Write("\n");
                }

                for (int i = 0; i < girdview.Rows.Count; i++)
                {
                    for (int j = 0; j < girdview.Columns.Count; j++)
                    {
                        w.Write(girdview.Rows[i].Cells[j].Text.Trim() + Convert.ToChar(9));
                    }
                    w.Write("\n");
                }
                w.Flush();
                w.Close();
            }
            catch
            {
                w.Close();
                return false;
            }
            return true;
        }

#9


好好学习~