列宽Excel文件 [英] Column Width Excel file

查看:52
本文介绍了列宽Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我从数据表创建了一个Excel文件,但是我在调​​整列大小时遇到​​问题,这是第一个问题.第二个是我该如何写一个特定的单元格(例如单元格(1,2),例如我需要用于标题的单元格).您能帮我吗?.这是我的代码

Hi,

I created an Excel file from a datatable but i have a problem to resize column, this is the first problem. The second is how can i write in a specific cell(for example the cell(1,2) I need this for the header for example).Can you help me?.This is my code

DataTable dt = (DataTable) ViewState["CurrentTable"];



            Workbook book = new Workbook();
            WorksheetStyle style = book.Styles.Add("Entete");
            style.Font.Bold = true;
            style.Font.Color = "White";
            style.Alignment.Horizontal = StyleHorizontalAlignment.Center;
            style.Interior.Color = "Blue";
            style.Interior.Pattern = StyleInteriorPattern.Solid;

            //create the first sheet
            Worksheet sheet = book.Worksheets.Add("Detail Seance");
            
            //The second sheet
            Worksheet sheetConf = book.Worksheets.Add("Total");

         
            //The header
            WorksheetRow row = sheet.Table.Rows.Add();
            row.Cells.Add(new WorksheetCell("Historique Exo"){ MergeAcross = 5 });
            
            row = sheet.Table.Rows.Add();
            Excel.Range range;
            
            foreach (DataColumn dc in dt.Columns)
            {
                row.Cells.Add(new WorksheetCell(dc.ColumnName, DataType.String, "Entete"));
                row.AutoFitHeight
            }

            int j;
            foreach (DataRow dr in dt.Rows)
            {
                row = sheet.Table.Rows.Add();
                for (j = 0; j < dt.Columns.Count; j++)
                {
                      row.Cells.Add(new WorksheetCell(dr[j].ToString()));
                    
                }
            }
            
            //save Excel file
            book.Save(Response.OutputStream); 


谢谢,

推荐答案

事物对:

-使用自动化创建Excel工作簿时,默认情况下3张工作表可用,因此除非必要,您实际上不需要将工作表添加到新工作簿中.
-同样,工作表具有已定义的行(2003版为64k,2007版及更高版本为100万行),因此无需添加行或单元格


您可以将整个工作表引用为二维数组(行,列),并直接通过以下方式对单元格进行写入/读取:
Couple of things:

- When you create an Excel workbook using automation, 3 sheets are available by default, so you do not actually need to add sheets to the new workbook unless necessary.
- Similarly, the worksheet(s) have the rows already defined (64k in case of version 2003 and ~1 million in versions 2007 and above), so there is no need to add rows or cells


You can reference the whole worksheet as a 2 dimensional array (Row,Column) and write/read direct to the cells as:
[Workbook object].Sheets(1).Cells(R,C) = Value



HTH



HTH


这篇关于列宽Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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