将文件保存在c#中的.xlsx中 [英] save file in .xlsx in c#

查看:94
本文介绍了将文件保存在c#中的.xlsx中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i wand浏览一个folde我选择目录和

当我输入名称时应保存文件.xlsx格式请帮忙..

hi

i wand to browse a folde and i select the directory and
when i enterd the name it should save tha file in .xlsx format please help on this..

推荐答案

参见使用MS Excel(xls / xlsx)使用MDAC和Oledb [ ^ ]。


您也可以了解OpenXML。



http://msdn.microsoft.com/en-us/library/office/bb448854.aspx [ ^ ]



你可以用它来访问/操纵以XML格式存储的office文档(xlsx / docx / xlsm等)。



如果你认为完整的OpenXML SDK对你来说太过分了,那么还有其他基于OpenXML的解决方案,它们更易于使用。我个人使用EPPlaus http://epplus.codeplex.com/ [ ^ ]
You may also learn about OpenXML.

http://msdn.microsoft.com/en-us/library/office/bb448854.aspx[^]

You can use it to access/manipulate office documents stored in XML format (xlsx/docx/xlsm etc).

If you feel that full OpenXML SDK is overkill for you, there are other solutions build on top of OpenXML that are simpler to use. I personally use EPPlaus http://epplus.codeplex.com/[^]


interface IExcel
{
    void Create();
    void SetData(int i, int j, string data);
    void SaveAs();
    void Release();
}

public class CBExcel : IExcel
{
    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    object misValue = System.Reflection.Missing.Value;
    const string ChartStart = "A1";
    string m_ChartEnd;
    int m_MaxI;
    int m_MaxJ;
    public CBExcel()
    {
        m_ChartEnd = "A1";
        m_MaxI = -1;
        m_MaxJ = -1;
    }

    public void SetData(int i, int j, string data)
    {
        xlWorkSheet.Cells[i, j] = data;
        CheckChartEnd(i, j);
    }

    private void CheckChartEnd(int i, int j)
    {
        if (m_MaxI <= i)
            m_MaxI = i;
        if (m_MaxJ <= j)
            m_MaxJ = j;
        const int a = 0x41;
        int word = a + j - 1;
        m_ChartEnd = string.Format("{0}{1}", Convert.ToChar(word), m_MaxI);
    }

    public void SetChart(Excel.XlChartType type)
    {
        Excel.Range chartRange;

        Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
        Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(500, 80, 350, 350);
        Excel.Chart chartPage = myChart.Chart;

        chartRange = xlWorkSheet.get_Range(ChartStart, m_ChartEnd);
        chartPage.SetSourceData(chartRange, misValue);
        chartPage.ChartType = type;
    }

    public void SetChart(string start, string end, Excel.XlChartType type)
    {
        Excel.Range chartRange;

        Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
        Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(500, 80, 350, 350);
        Excel.Chart chartPage = myChart.Chart;

        chartRange = xlWorkSheet.get_Range(start, end);
        chartPage.SetSourceData(chartRange, misValue);
        chartPage.ChartType = type;
    }

    private void releaseObject(object obj)
    {
        try
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
            obj = null;
        }
        catch
        {
            obj = null;
        }
        finally
        {
            GC.Collect();
        }
    }

    public void Create()
    {
        xlApp = new Excel.ApplicationClass();
        xlWorkBook = xlApp.Workbooks.Add(misValue);
        xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    }

    public void SaveAs()
    {
        SetChart(Excel.XlChartType.xlLine);
        xlWorkBook.Close(true, misValue, misValue);
        xlApp.Quit();
    }

    public void Release()
    {
        releaseObject(xlWorkSheet);
        releaseObject(xlWorkBook);
        releaseObject(xlApp);
    }
}



您可以使用此代码保存Excel。


You can use this code to save excel.

private void button1_Click(object sender, EventArgs e)
{
    CBExcel excel = new CBExcel();
    excel.Create(); // create Excel
    excel.SetData(1, 1, ""); // set data
    excel.SetData(1, 2, "Student1");
    excel.SetData(1, 3, "Student2");
    excel.SetData(1, 4, "Student3");

    excel.SetData(2, 1, "Term1");
    excel.SetData(2, 2, "80");
    excel.SetData(2, 3, "65");
    excel.SetData(2, 4, "45");

    excel.SetData(3, 1, "Term2");
    excel.SetData(3, 2, "81");
    excel.SetData(3, 3, "61");
    excel.SetData(3, 4, "41");

    excel.SetData(4, 1, "Term3");
    excel.SetData(4, 2, "82");
    excel.SetData(4, 3, "62");
    excel.SetData(4, 4, "42");

    excel.SetChart("A1", "D4", Excel.XlChartType.xlLine);
    excel.SaveAs(); // save Excel
    excel.Release();// release
}


这篇关于将文件保存在c#中的.xlsx中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆