/** * Reads in the inputFile and creates a writable copy of it called outputFile * * @exception IOException * @exception BiffException */ public void readWrite() { Console.WriteLine("Reading..."); Workbook w1 = Workbook.getWorkbook(inputWorkbook); Console.WriteLine("Copying..."); WritableWorkbook w2 = Workbook.createWorkbook(outputWorkbook, w1); if (inputWorkbook.Name == "jxlrwtest.xls") { modify(w2); } w2.write(); w2.close(); Console.WriteLine("Done"); }
public byte[] Render(Rdl.Render.GenericRender report, bool renderAll) { _report = report; MemoryStream ms = new MemoryStream(); report.SetSizes(renderAll); _workbook = Workbook.createWorkbook(ms); //_workbook = Workbook.createWorkbook(new System.IO.FileInfo(@"c:\foo.xls")); _ws = _workbook.createSheet("Sheet 1", 0); RecurseBuildRowsCols(report.BodyContainer, 0, 0, renderAll); _rows.Add(0); _rows.Sort(delegate(decimal d1, decimal d2) { return(decimal.Compare(d1, d2)); }); _cols.Add(0); _cols.Sort(delegate(decimal d1, decimal d2) { return(decimal.Compare(d1, d2)); }); for (int i = 1; i < _rows.Count; i++) { _ws.setRowView(i - 1, (int)((_rows[i] - _rows[i - 1]) * _lineHeight)); } for (int i = 1; i < _cols.Count; i++) { _ws.setColumnView(i - 1, (int)((_cols[i] - _cols[i - 1]) * _colWidth)); } formats = new WritableCellFormat[report.StyleList.Count]; for (int i = 0; i < report.StyleList.Count; i++) { if (report.StyleList[i] is TextStyle) { formats[i] = GetWritableFormat((TextStyle)report.StyleList[i]); } } RecurseRender(report.BodyContainer, 0, 0, renderAll); _workbook.write(); _workbook.close(); //BIFF8Writer.WriteWorkbookToStream(_workbook, ms); return(ms.ToArray()); }
// =========================================================================== // 功能區 // =========================================================================== /// <summary> /// 產生Excel /// </summary> /// <param name="exportConfigInfo"> </param> /// <param name="exportDataSet"> </param> /// <param name="outString"></param> public virtual void export(ExportConfigInfo exportConfigInfo, ExportDataSet exportDataSet, Stream outString) { configInfo = exportConfigInfo; try { // ========================================================= // 建立 Workbook // ========================================================= WritableWorkbook writableWorkbook = Workbook.createWorkbook(outString); for (int sheetlIndex = 0; sheetlIndex < exportConfigInfo.SheetList.Count; sheetlIndex++) { // ===================================================== // 建立 sheet // ===================================================== // 取得 sheetlInfo 設定 SheetlInfo sheetlInfo = exportConfigInfo.SheetList[sheetlIndex]; // 取得 sheetName string sheetName = (ExcelStringUtil.IsEmpty(sheetlInfo.SheetName)) ? "Sheet" + (sheetlIndex + 1) : sheetlInfo.SheetName; // 建立 sheet WritableSheet writableSheet = writableWorkbook.createSheet(sheetName, sheetlIndex); // 版面設定 // setPageSetup Parameters: // p - the page orientation // ps - the paper size // hm - the header margin, in inches // fm - the footer margin, in inches writableSheet.setPageSetup(PageOrientation.LANDSCAPE, exportConfigInfo.PaperSize, 0, 0); writableSheet.getSettings().setLeftMargin(0); writableSheet.getSettings().setRightMargin(0); // ===================================================== // 處理前準備 // ===================================================== // 列指標 int targetRowIndex = 0; // 紀錄已使用的儲存格 (cell) var usedCells = new Dictionary <int, HashSet <string> >(); // 紀錄欄(column)的最大欄寬 var maxWidthMap = new Dictionary <string, int>(); // ===================================================== // 資訊 // ===================================================== foreach (var entry in sheetlInfo.PartInfoMap) { if (entry.Value == null) { return; } // 內容為 context if (entry.Key.StartsWith(Constant.ELEMENT_CONTEXT)) { //取得 context 設定檔設定資料 var contextInfo = (ContextInfo)entry.Value; //取得 匯出資料 Dictionary <string, object> dataMap = exportDataSet.getContext(contextInfo.DataId); targetRowIndex = WriteContext( writableSheet, contextInfo, targetRowIndex, dataMap, usedCells, maxWidthMap); } //內容為 detail if (entry.Key.StartsWith(Constant.ELEMENT_DETAIL)) { //取得 context 設定檔設定資料 var detailInfo = (DetailInfo)entry.Value; //取得 匯出資料 var columnDataSetList = exportDataSet.getDetail(detailInfo.DataId); targetRowIndex = EnterWriteDetail( writableSheet, detailInfo, targetRowIndex, columnDataSetList, usedCells, maxWidthMap); } } // ===================================================== // 設定欄寬 // ===================================================== // 取得最大欄位 index int maxColIndex = maxWidthMap[KEY_MAX_COL]; for (int colIndex = 0; colIndex <= maxColIndex; colIndex++) { // 取得欄寬 int colWidth = 0; //取得 MAP 中的值 (tr、td 設定) if (maxWidthMap.ContainsKey(colIndex + "")) { colWidth = maxWidthMap[colIndex + ""]; } //若 tr td 未設定時,取 style 設定 if (colWidth == 0) { colWidth = Convert.ToInt32(ExcelStringUtil.SafeTrim(exportConfigInfo.StyleInfo.Width, "0")); } // 以上都未設定時使用預設值 //if (colWidth == 0) //{ // colWidth = Convert.ToInt32(Constant.DEFAULT_WIDTH); //} if (colWidth > 0) { writableSheet.setColumnView(colIndex, colWidth); } } } writableWorkbook.write(); writableWorkbook.close(); } catch (Exception e) { throw new ExcelOperateException("EXCEL 檔案匯出處理錯誤! \r\n" + e.Message + "\r\n" + e.StackTrace); } }